Jump to content

Recommended Posts

I was thinking how can I make a script that will build strings incrementing through each letter starting with "a" and ending with "zzzzzz" or what ever ending length I want such as 12.

 

So if your were to output everything, you could find the words:

"dog"

"spider"

"superman"

"hippo"

etc

 

What would be the best way to do this, so I don't have to have a loop that is 500 blocks deep to show a word that is 500 letters

 

example

a

...

z

aa

ab

ac

...

az

ba

bb

bc

...

...

aaa

aab

aac

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/
Share on other sites

Maybe this example will help. Just follow the pattern:

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
aa
ab
ac
ad
ae
af
ag
ah
ai
aj
ak
al
am
an
ao
ap
aq
ar
as
at
au
av
aw
ax
ay
az
ba
bb
bc
bd
be
...
aaa
aab
aac
aad
aae
aaf
....
aba
abb
abc
abd
.....
aaaa
aaab
aaac
....
abaa
abab
abac
....

 

basically it walks its way through every string combination possible (using a looping method) with a string no longer than the defined max length and displaying them all on the screen (CLI).

 

so, if there was a string max length of 10 and only using a-z lowercase there is a maximum of 141,167,095,653,376 combinations (26^10)

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1155992
Share on other sites

Well I could see wanting to generate maybe words or numbers at random. But like the above persons said expect the server to just puke,exhaust, time out or w/e else with trying that many.

 

It's just too many to produce.

 

Now maybe if did in itsy bitsy steps and started saving either to text files or a database, then maybe you can.

 

I sat here writing up something you can at least play with to try to suit your needs.

Maybe it can give you some ideas, the end result I limited it to just showing words with length of 3, so if changed that to 2 or 1 would see the others.

 

<?php
$positions = 1;
$i = 0;
for ($i = 0;$i<$positions;$i++) {
  foreach ( range('a','z') as $alphabet) {
  $the_alphabet[] =  $alphabet;
}

}
if (++$i == $positions) break;

foreach($the_alphabet as $k1 =>$v1){
    $words[]=$v1;
    foreach($the_alphabet as $k2 => $v2){
        $words[]=$v2;
        $words[]="$v1$v2";
        foreach($the_alphabet as $k3 => $v3){
            $words[]=$v3;
            $words[]="$v1$v2$v3";
            $words[]="$v1$v3$v2";
        }
    }
}
$words = array_unique($words);
sort($words);
foreach($words as $word){
$word_length = strlen($word);
if ($word_length == 3) { //try 2 or 1
    echo "$word<br>";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156050
Share on other sites

for($i= 'a'; $i <'zz'; $i++) echo $i;

 

Haha! If that only really worked ;)

 

Try:

for($i= 'a'; $i < 'z'; $i++) echo $i;

 

And now run:

for($i= 'a'; $i <= 'z'; $i++) echo $i;

 

Produces suddenly the double number of results (and orders incorrectly) ;) The answer lies in it's implementation :D

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156286
Share on other sites

for($i= 'a'; $i <'zz'; $i++) echo $i;

not tested

 

output:

abcdefghijklmnopqrstuvwxyzaaabacadaeafagahaiajakalamanaoapaqarasatauavawaxayazbabbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzcacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczdadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzeaebecedeeefegeheiejekelemeneoepeqereseteuevewexeyezfafbfcfdfefffgfhfifjfkflfmfnfofpfqfrfsftfufvfwfxfyfzgagbgcgdgegfggghgigjgkglgmgngogpgqgrgsgtgugvgwgxgygzhahbhchdhehfhghhhihjhkhlhmhnhohphqhrhshthuhvhwhxhyhziaibicidieifigihiiijikiliminioipiqirisitiuiviwixiyizjajbjcjdjejfjgjhjijjjkjljmjnjojpjqjrjsjtjujvjwjxjyjzkakbkckdkekfkgkhkikjkkklkmknkokpkqkrksktkukvkwkxkykzlalblcldlelflglhliljlklllmlnlolplqlrlsltlulvlwlxlylzmambmcmdmemfmgmhmimjmkmlmmmnmompmqmrmsmtmumvmwmxmymznanbncndnenfngnhninjnknlnmnnnonpnqnrnsntnunvnwnxnynzoaobocodoeofogohoiojokolomonooopoqorosotouovowoxoyozpapbpcpdpepfpgphpipjpkplpmpnpopppqprpsptpupvpwpxpypzqaqbqcqdqeqfqgqhqiqjqkqlqmqnqoqpqqqrqsqtquqvqwqxqyqzrarbrcrdrerfrgrhrirjrkrlrmrnrorprqrrrsrtrurvrwrxryrzsasbscsdsesfsgshsisjskslsmsnsospsqsrssstsusvswsxsysztatbtctdtetftgthtitjtktltmtntotptqtrtstttutvtwtxtytzuaubucudueufuguhuiujukulumunuoupuqurusutuuuvuwuxuyuzvavbvcvdvevfvgvhvivjvkvlvmvnvovpvqvrvsvtvuvvvwvxvyvzwawbwcwdwewfwgwhwiwjwkwlwmwnwowpwqwrwswtwuwvwwwxwywzxaxbxcxdxexfxgxhxixjxkxlxmxnxoxpxqxrxsxtxuxvxwxxxyxzyaybycydyeyfygyhyiyjykylymynyoypyqyrysytyuyvywyxyyyzzazbzczdzezfzgzhzizjzkzlzmznzozpzqzrzsztzuzvzwzxzy

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156311
Share on other sites

for($i= 'a'; $i <'zz'; $i++) echo $i;

not tested

 

output:

abcdefghijklmnopqrstuvwxyzaaabacadaeafagahaiajakalamanaoapaqarasatauavawaxayazbabbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzcacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczdadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzeaebecedeeefegeheiejekelemeneoepeqereseteuevewexeyezfafbfcfdfefffgfhfifjfkflfmfnfofpfqfrfsftfufvfwfxfyfzgagbgcgdgegfggghgigjgkglgmgngogpgqgrgsgtgugvgwgxgygzhahbhchdhehfhghhhihjhkhlhmhnhohphqhrhshthuhvhwhxhyhziaibicidieifigihiiijikiliminioipiqirisitiuiviwixiyizjajbjcjdjejfjgjhjijjjkjljmjnjojpjqjrjsjtjujvjwjxjyjzkakbkckdkekfkgkhkikjkkklkmknkokpkqkrksktkukvkwkxkykzlalblcldlelflglhliljlklllmlnlolplqlrlsltlulvlwlxlylzmambmcmdmemfmgmhmimjmkmlmmmnmompmqmrmsmtmumvmwmxmymznanbncndnenfngnhninjnknlnmnnnonpnqnrnsntnunvnwnxnynzoaobocodoeofogohoiojokolomonooopoqorosotouovowoxoyozpapbpcpdpepfpgphpipjpkplpmpnpopppqprpsptpupvpwpxpypzqaqbqcqdqeqfqgqhqiqjqkqlqmqnqoqpqqqrqsqtquqvqwqxqyqzrarbrcrdrerfrgrhrirjrkrlrmrnrorprqrrrsrtrurvrwrxryrzsasbscsdsesfsgshsisjskslsmsnsospsqsrssstsusvswsxsysztatbtctdtetftgthtitjtktltmtntotptqtrtstttutvtwtxtytzuaubucudueufuguhuiujukulumunuoupuqurusutuuuvuwuxuyuzvavbvcvdvevfvgvhvivjvkvlvmvnvovpvqvrvsvtvuvvvwvxvyvzwawbwcwdwewfwgwhwiwjwkwlwmwnwowpwqwrwswtwuwvwwwxwywzxaxbxcxdxexfxgxhxixjxkxlxmxnxoxpxqxrxsxtxuxvxwxxxyxzyaybycydyeyfygyhyiyjykylymynyoypyqyrysytyuyvywyxyyyzzazbzczdzezfzgzhzizjzkzlzmznzozpzqzrzsztzuzvzwzxzy

 

I knew it'd work, see :D

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156313
Share on other sites

for($i= 'a'; $i <'zz'; $i++) echo $i;

not tested

 

output:

abcdefghijklmnopqrstuvwxyzaaabacadaeafagahaiajakalamanaoapaqarasatauavawaxayazbabbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzcacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczdadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzeaebecedeeefegeheiejekelemeneoepeqereseteuevewexeyezfafbfcfdfefffgfhfifjfkflfmfnfofpfqfrfsftfufvfwfxfyfzgagbgcgdgegfggghgigjgkglgmgngogpgqgrgsgtgugvgwgxgygzhahbhchdhehfhghhhihjhkhlhmhnhohphqhrhshthuhvhwhxhyhziaibicidieifigihiiijikiliminioipiqirisitiuiviwixiyizjajbjcjdjejfjgjhjijjjkjljmjnjojpjqjrjsjtjujvjwjxjyjzkakbkckdkekfkgkhkikjkkklkmknkokpkqkrksktkukvkwkxkykzlalblcldlelflglhliljlklllmlnlolplqlrlsltlulvlwlxlylzmambmcmdmemfmgmhmimjmkmlmmmnmompmqmrmsmtmumvmwmxmymznanbncndnenfngnhninjnknlnmnnnonpnqnrnsntnunvnwnxnynzoaobocodoeofogohoiojokolomonooopoqorosotouovowoxoyozpapbpcpdpepfpgphpipjpkplpmpnpopppqprpsptpupvpwpxpypzqaqbqcqdqeqfqgqhqiqjqkqlqmqnqoqpqqqrqsqtquqvqwqxqyqzrarbrcrdrerfrgrhrirjrkrlrmrnrorprqrrrsrtrurvrwrxryrzsasbscsdsesfsgshsisjskslsmsnsospsqsrssstsusvswsxsysztatbtctdtetftgthtitjtktltmtntotptqtrtstttutvtwtxtytzuaubucudueufuguhuiujukulumunuoupuqurusutuuuvuwuxuyuzvavbvcvdvevfvgvhvivjvkvlvmvnvovpvqvrvsvtvuvvvwvxvyvzwawbwcwdwewfwgwhwiwjwkwlwmwnwowpwqwrwswtwuwvwwwxwywzxaxbxcxdxexfxgxhxixjxkxlxmxnxoxpxqxrxsxtxuxvxwxxxyxzyaybycydyeyfygyhyiyjykylymynyoypyqyrysytyuyvywyxyyyzzazbzczdzezfzgzhzizjzkzlzmznzozpzqzrzsztzuzvzwzxzy

 

I knew it'd work, see :D

 

Now explain why it does this ;)

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156336
Share on other sites

I decided to try and throw some symbols, upper case letters and lower case letters in there as well... Doesn't work too well:

$start = 32;
$fin = 126;
$depth = 1;

$i = $start;
while(true){
$opt = '';
for($d=0;$d<=$depth;$d++){
	$opt .= chr($i);
	if($d == $depth){
		$depth++;
		break;
	}
}
echo $opt."\n";
$i++;
}

 

Oh, and btw if you don't want it to end:

$i = 'a';
while($i){
echo $i."\n";
$i++;
}

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156337
Share on other sites

Well here's the best way by far.

I guess can even do sets of the 26 letters as well, something like show single letters, then show 2 letters, then show 3 letters and so on , but just do them in ranges so don't have to produce what you don't need.

<?php
$how_many_you_want = 50000;//each number will add an extra line/sequential alphabet string

function seqAlphabet($numbers)
{
  $alphabet = '';
  while($numbers >= 1) {
    $numbers = $numbers - 1;
    $alphabet = chr(($numbers % 26)+97) . $alphabet;
    $numbers = $numbers / 26;
  }
  return $alphabet;
}
$string_position = range('1',$how_many_you_want);
foreach ($string_position as $position) {
echo seqAlphabet($position) . "<br />";
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156348
Share on other sites

Using this:

$i = 'a';
$c = 1;
while(true){
echo $c.'. '.$i."\n";
if($i == 'ryan')
	exit;
$i++;
$c++;
}

 

This is how long it took to find my name :)

 

333308. ryan

 

See how long it takes to find my name: Dromodor Kastalian

It doesn't handle spaces

 

That's ok. You can just use my native first name: dromodorialmakirvahumvete

Link to comment
https://forums.phpfreaks.com/topic/223623-building-strings/#findComment-1156382
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.