0

I'm trying to generate strings in PHP like

"A" next "B"..."Z" next "ZA" next "ZB"..."ZZ" next "ZZA" next "ZZB"..."ZZZ" next "ZZZA"

Could someone help me with a method?

djramc
  • 1
  • 5
  • Please share your code. – common sense Jan 21 '18 at 12:34
  • I wrote a script back in the day that did this and it turned out to be the most efficient way, if I find it i'll post it. Have a look for a script you might find it. – Jack Hales Jan 21 '18 at 12:35
  • @Jek interesting. Please post if you find it. Not sure how it can be done more efficient then posted below. – Andreas Jan 21 '18 at 12:53
  • first link above is not the same . second link is nearly i changed the code little bit – djramc Jan 21 '18 at 13:13
  • Well, @djramc, it was meant to follow certain criteria and it was for an old hackathon I attended. It was to build the underlying string generator for a brute force, generating every possible string as efficiently as possible. – Jack Hales Jan 21 '18 at 23:48

2 Answers2

1

simply use increment operator -

$a = "A";
for($i=0;$i<100;$i++){
        echo $a++.' ';
}

just run this code to see the magic.

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

Sohel0415
  • 9,523
  • 21
  • 30
0

PHP can count in letters.

For($i='A'; $i<='ZZZA'; $i++){
   Echo $i ."\n";
}

https://3v4l.org/VgLi8

Andreas
  • 23,610
  • 6
  • 30
  • 62