How to write all possible words in php?

Possible duplicate:
Generate all combinations of an arbitrary alphabet to an arbitrary length

I am trying to write all possible 10 letter words (zzzzzzzzzz) in php. How can i do this? it will look like this: http://i.imgur.com/sgUnL.png

I tried several ways, but they only do 10 letters randomly, not increasing from 1 letter. By the way, runtime and how big it is is not a problem. I just need an algorithm for it, if someone shows it with code, it will be more useful, of course.

+4
source share
3 answers

Version 1:

for($s = 'a'; $s <= 'zzzzzzzzzz'; print $s++.PHP_EOL); 

as Paul noted in the comments below, this will only be zzzzzzzzyz . A bit slower (if someone cares), but the correct version is:

 //modified to include arnaud576875 method of checking exit condition for($s = 'a'; !isset($s[10]); print $s++.PHP_EOL); 
+6
source
 function words($length, $prefix='') { if ($length == 0) return; foreach(range('a', 'z') as $letter) { echo $prefix . $letter, "\n"; words($length-1, $prefix . $letter); } } 

Using:

 words(10); 

Try it here: http://codepad.org/zdTGLtjY (with words up to 3 letters)

+7
source
  <?php function makeWord($length, $prefix='') { if ($length <= 0) { echo $prefix . "\n"; return; } foreach(range('a', 'z') as $letter) { makeWord($length - 1, $prefix . $letter); } } // Use the function to write the words. $minSize = 1; $maxSize = 3; for ($i = $minSize; $i <= $maxSize; $i++) { makeWord($i); } ?> 
0
source

All Articles