...

View Full Version : Easy Question: Character Loop



lepoor
06-19-2006, 08:27 PM
What's the best way to have PHP generate a loop that outputs every combination of the alphabet letters given a maximum length.

Ex. Say the max is 5

Output: A B C.... ZZZZZ

Lepoor

fci
06-19-2006, 08:44 PM
for ($x='A'; strlen($x)<=5; $x++)
print $x.' ';

lavinpj1
06-19-2006, 08:55 PM
Nice code fci. It did however time out for me, as it took more than 30 secs to execute. Would be rather heavy on the server, so use it wisely.

~Phil~

fci
06-19-2006, 09:28 PM
took me about a minute to run it into a file
<?php

print date('r').'<br />';
$fp = fopen('abc.txt', 'wb+');
for ($x='A'; strlen($x)<=5; $x++)
fwrite($fp, $x."\r\n");
fclose($fp);
print date('r').'<br />';

/*
Mon, 19 Jun 2006 14:19:40 -0500
Mon, 19 Jun 2006 14:20:36 -0500
*/
?>




...creating bigger chunks makes it take less time though:

<?php

print date('r').'<br />';
$fp = fopen('abc.txt', 'wb+');
$mod = 1000;
$tmp = '';
for ($z='',$x='A'; strlen($x)<=5; $x++, $z++) {
if ($z%$mod==0) {
fwrite($fp, $tmp);
$tmp ='';
} else {
$tmp .= "$x\r\n";
}
}
if ($z%$mod==0)
fwrite($fp, $tmp);

fclose($fp);
print date('r').'<br />';

/*
Mon, 19 Jun 2006 14:27:29 -0500
Mon, 19 Jun 2006 14:27:50 -0500
*/
?>

marek_mar
06-19-2006, 11:10 PM
I really wonder what this might be for... If you use the 26 character set with length 5 you get 11 881 376 combinations (quite a bunch) with ten it's ~1,4116 * 10^14... say you do a milion combinations per second (fci's faster attempt did ~550 000/second)... and it takes you ~4,5years.
Do you still wonder why it timed out lavinpj1?

lavinpj1
06-20-2006, 08:49 AM
I didn't WONDER why it timed out. I merely pointed out that it should be used wisely due to the high server load.

~Phil~

nirali35
04-09-2007, 05:06 PM
I am interested to get similar results.

But I want all combinations of the $Char up to 5 char.



$Char = 'abcdefghijklmnopqrstuvwxyz';
$Char .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$Char .= '`~!@#$%^&*()_-+=[]\|}{;:\'",./<>?';


Any help please?


for ($x='A'; strlen($x)<=5; $x++)
print $x.' ';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum