使用 php 在大小写循环中增加字母

发布于 2024-10-14 16:42:46 字数 106 浏览 5 评论 0原文

我正在开发一个项目,我需要循环显示这样的字母:a,A,b,B ... z,Z,aa,Aa,aA,AA,bb ... zz,ZZ ... PHP。

是否有可能以可扩展的方式做到这一点?

I am working on a project where I need to cycle through letters 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北方。的韩爷 2024-10-21 16:42:46

不确定这就是您要找的吗?

$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;
  }
}

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;
  }
}
像你 2024-10-21 16:42:46

计算排列是一项相当繁重的任务。这将专门针对计算包含所有小写和大写字母的集合的两个字符排列的情况(我还没有测试该函数,但您应该能够弄清楚它在做什么并修复任何错误,它会至少让你开始)。

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)));
}

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)));
}
青朷 2024-10-21 16:42:46

$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,

$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,
盛夏已如深秋| 2024-10-21 16:42:46

此代码写入一行包含 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,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,
aaa,Aaa,aAa,AAa,aaA,AaA,aAA,AAA,bbb,Bbb,bBb,BBb,bbB,BbB,bBB,BBB,ccc,CCC,ccCc,CCc,ccC,CCC,ccC,CCC,ddd, ddd、ddd、ddd、ddd、ddd、ddd、ddd、eee、eee、eee、eee、eee、eee、eeee、eee、fff、fff、fff、fff、fff、fff、fff、fff、ggg、ggg, ggg,ggg,ggG,ggg,ggg,ggg,hhh,hhhh,hHh,HHh,hhH,HhH,hHH,HHH,iii,Iii,iIi,IIIi,iii,IiI,iII,III,jjj,Jjj,jJj, JJj,jjJ,JjJ,jJJ,JJJ,kkk,Kkk,kKk,KKk,kkK,KkK,kKK,KKK,lll,Lll,lLl,LLl,llL,LlL,lLL,LLL,mmm,Mmm,mMm,MMm, mmM、MmM、mMM、MMM、nnn、Nnn、nNn、NNn、nnN、NnN、nNN、NNN、ooo、Ooo、oOo、OOo、ooO、OoO、oOO、OOO、ppp、ppp、ppp、ppp、ppp、 PpP,pPP,PPP,qqq,Qqq,qQq,QQq,qQQ,QqQ,qQQ,QQQ,rrr,Rrr,rRr,RRr,rrR,RrR,rRR,RRR,sss,Sss,sSs,SSs,ssS,SsS, sSS,SSS,ttt,Ttt,tTt,TTt,ttT,TtT,tTT,TTT,uuu,Uuu,uUu,UUu,uuU,UuU,uUU,UUU,vvv,Vvv,vVv,VVv,vvV,VvV,vVV, VVV,www,WWW,WWW,WWW,WWW,WWW,WWW,WWW,xxx,Xxx,xXx,XXx,xxX,XxX,xXX,XXX,yyy,Yyy,yYy,YYy,yyY,YyY,yYY,YYY, zzz,Zzz,zZz,ZZz,zzZ,ZzZ,zZZ,ZZZ,

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,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,
aaa,Aaa,aAa,AAa,aaA,AaA,aAA,AAA,bbb,Bbb,bBb,BBb,bbB,BbB,bBB,BBB,ccc,Ccc,cCc,CCc,ccC,CcC,cCC,CCC,ddd,Ddd,dDd,DDd,ddD,DdD,dDD,DDD,eee,Eee,eEe,EEe,eeE,EeE,eEE,EEE,fff,Fff,fFf,FFf,ffF,FfF,fFF,FFF,ggg,Ggg,gGg,GGg,ggG,GgG,gGG,GGG,hhh,Hhh,hHh,HHh,hhH,HhH,hHH,HHH,iii,Iii,iIi,IIi,iiI,IiI,iII,III,jjj,Jjj,jJj,JJj,jjJ,JjJ,jJJ,JJJ,kkk,Kkk,kKk,KKk,kkK,KkK,kKK,KKK,lll,Lll,lLl,LLl,llL,LlL,lLL,LLL,mmm,Mmm,mMm,MMm,mmM,MmM,mMM,MMM,nnn,Nnn,nNn,NNn,nnN,NnN,nNN,NNN,ooo,Ooo,oOo,OOo,ooO,OoO,oOO,OOO,ppp,Ppp,pPp,PPp,ppP,PpP,pPP,PPP,qqq,Qqq,qQq,QQq,qqQ,QqQ,qQQ,QQQ,rrr,Rrr,rRr,RRr,rrR,RrR,rRR,RRR,sss,Sss,sSs,SSs,ssS,SsS,sSS,SSS,ttt,Ttt,tTt,TTt,ttT,TtT,tTT,TTT,uuu,Uuu,uUu,UUu,uuU,UuU,uUU,UUU,vvv,Vvv,vVv,VVv,vvV,VvV,vVV,VVV,www,Www,wWw,WWw,wwW,WwW,wWW,WWW,xxx,Xxx,xXx,XXx,xxX,XxX,xXX,XXX,yyy,Yyy,yYy,YYy,yyY,YyY,yYY,YYY,zzz,Zzz,zZz,ZZz,zzZ,ZzZ,zZZ,ZZZ,

如果没有 2024-10-21 16:42:46

当然。字母字符只需使用 $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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文