Using php to increment letters while cycling through capitilisations
I am working on a project where I need to cycle through let开发者_如何学编程ters something like this: a,A,b,B...z,Z,aa,Aa,aA,AA,bb...zz,ZZ... in PHP.
Is it possible to do this in a scalable way?
Not sure it this what you looking for ?
$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;
}
}
Calculating permutations is a pretty heavy task. This will do it specifically for your case of calculating two character permutations of the set containing all lower and upper case letters (I haven't tested the function but you should be able to work out what it's doing an fix any errors, it'll get you started at least).
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)));
}
$lowercase
and $uppercase
are arrays that hold the ASCII values for a-z and A-Z, respectively.
Modify as necessary.
<?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 ", ";
}
?>
This is what the output looks like:
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,
This code writes one line with elements 1 char wide, then one with 2 chars wide, etc.
For each width, it prints all the permutations by iterating the letters and counting from 0 to 2 ^ width and capitalizing based on binary bits.
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)
The code:
$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";
}
output:
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,j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yY,yYY,YYY,zzz,Zzz,zZz,ZZz,zzZ,ZzZ,zZZ,ZZZ,
Sure. Alpha characters can be incremented with just $a++
. For simplicity's sake if just a single or double length set is needed, you can do...
$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
精彩评论