我正在做一个项目,我需要在这样的字母中循环:a,A,b,B...z,Z,aa,Aa,aA,AA,bb...zz,ZZ... in php。
是否有可能以可扩展的方式做到这一点?
计算排列是一项相当繁重的任务。这将专门针对您计算包含所有小写和大写字母的集合的两个字符排列的情况(我尚未测试该功能,但您应该能够弄清楚它在做什么以修复任何错误,它会至少让你开始)。
function permutations($items)
{
empty($items)?return array():$perms = array();
for ($i = count($items)-1; $i > 0; --$i)
{
$perms[] = $items[0].$items[$i];
}
return array_merge($perms, permutations(array_slice($items,1)));
}
不确定这是您要找的吗?
$low = $low2 = range('a', 'z');
$upp = $upp2 = range('A', 'Z');
$arr = array();
foreach ($low as $val)
{
$arr[] = $val;
foreach ($low2 as $lval)
{
$arr[] = $val.$lval;
}
}
foreach ($upp as $val)
{
$arr[] = $val;
foreach ($upp2 as $uval)
{
$arr[] = $val.$uval;
}
}
foreach ($low as $val)
{
foreach ($upp as $uval)
{
$arr[] = $val.$uval;
}
}
foreach ($upp as $val)
{
foreach ($low as $lval)
{
$arr[] = $val.$lval;
}
}
$lowercase
并且$uppercase
是分别保存 az 和 AZ 的 ASCII 值的数组。
根据需要进行修改。
<?php
$lowercase = array(97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122);
$uppercase = array(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90);
$i = 0;
for($i; $i < 26; $i++) {
echo chr($lowercase[$i]) . ", ";
echo chr($uppercase[$i]) . ", ";
}
$i = 0;
for($i; $i < 26; $i++) {
echo chr($lowercase[$i]);
echo chr($lowercase[$i]);
echo ", ";
echo chr($uppercase[$i]);
echo chr($lowercase[$i]);
echo ", ";
echo chr($lowercase[$i]);
echo chr($uppercase[$i]);
echo ", ";
echo chr($uppercase[$i]);
echo chr($uppercase[$i]);
echo ", ";
}
?>
这是输出的样子:
a, A, b, B, c, C, d, D, e, E, f, F, g, G, h, H, i, I, j, J, k, K, l, L, m, M, n, N, o, O, p, P, q, Q, r, R, s, S, t, T, u, U, v, V, w, W, x, X, y, Y, z, Z, aa, Aa, aA, AA, bb, Bb, bB, BB, cc, Cc, cC, CC, dd, Dd, dD, DD, ee, Ee, eE, EE, ff, Ff, fF, FF, gg, Gg, gG, GG, hh, Hh, hH, HH, ii, Ii, iI, II, jj, Jj, jJ, JJ, kk, Kk, kK, KK, ll, Ll, lL, LL, mm, Mm, mM, MM, nn, Nn, nN, NN, oo, Oo, oO, OO, pp, Pp, pP, PP, qq, Qq, qQ, QQ, rr, Rr, rR, RR, ss, Ss, sS, SS, tt, Tt, tT, TT, uu, Uu, uU, UU, vv, Vv, vV, VV, ww, Ww, wW, WW, xx, Xx, xX, XX, yy, Yy, yY, YY, zz, Zz, zZ, ZZ,
这段代码写一行,元素宽度为 1 个字符,然后一行元素宽度为 2 个字符,以此类推。
对于每个宽度,它通过迭代字母并从 0 到 2 ^ 宽度计数并基于二进制位大写来打印所有排列。
e.g.
00 = aa
01 = aA
10 = Aa
11 = AA
(actually I have iterated bits from lowest to highest rank to get the order to match your example)
编码:
$maxwidth = 3;
for ( $width = 1; $width <= $maxwidth; $width++ ) {
for ( $letter = ord('a'); $letter <= ord('z'); $letter++ ) {
for ( $num = 0; $num < pow(2,$width); $num++ ) {
for ( $bit = 0; $bit < $width; $bit++ ) {
print ($num & pow(2, $bit)) ? strtoupper(chr($letter)) : chr($letter);
}
print ',';
}
}
print "\n";
}
输出:
a,A,b,B,c,C,d,D,e,E,f,F,g,G,h,H,i,I,j,J,k,K,l,L,m, M,n,N,o,O,p,P,q,Q,r,R,s,S,t,T,u,U,v,V,w,W,x,X,y,Y, z,Z,aa,Aa,aA,AA,bb,Bb,bB,BB,cc,Cc,cC,CC,dd,Dd,dD,DD,ee,Ee,eE,EE,ff,Ff,fF, FF,gg,Gg,gG,GG,hh,Hh,hH,HH,ii,Ii,iI,II,jj,Jj,jJ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YyY,yYY,YYY,zzz,Zzz,zZz,ZZz,zzZ,ZzZ,zZZ,ZZZ,
当然。字母字符可以只增加$a++
. 为简单起见,如果只需要单长度或双长度集,您可以...
$single_set = array(); // array holding single-character sequence
$double_set = array(); // array holding double-character sequence
for ($v1 = 'a', $v2 = 'A'; $v1 !== 'aa'; $v1++, $v2++) {
$single_set[] = $v1;
$single_set[] = $v2;
$double_set[] = $v1 . $v1;
$double_set[] = $v1 . $v2;
$double_set[] = $v2 . $v1;
$double_set[] = $v2 . $v2;
}
$set = $single_set + $double_set; // combined array sequence