This is how I would do it:
$lower = true; //if you want lowercase letters
$t = 4;
$s = 1;
while( $s <= $t ) {
$i = ( $lower ? 97 : 65 );
while( $i <= ( $lower ? 122 : 90 ) ) {
echo str_repeat( chr( $i ),$s ) . '';
$i++;
}
$s++;
}
That what you are talking about?
kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 195
This code below will do the trick, but it's going to use a lot of memory to run this.
You are making 26 x 26 x 26 x 26 = 456976 combinations. Are you sure you want to generate all of these combinations? What are you trying to achieve this for?
<?
for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
for($b = 97; $b <= 122; $b++)
{
for($c = 97; $c <= 122; $c++)
{
for($d = 97; $d <= 122; $d++)
{
echo chr($a).chr($b).chr($c).chr($d)."<br/>";
}
}
}
}
?>
SimonMayer
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 14
Solved Threads: 10
In fact, I realise that the above code will not produce your single, double and triple character strings.
There are more elegant ways of doing it, but I am not able to put the time into devising one at the moment. One way would be just to repeat the code like so:
<?
for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
echo chr($a)."<br/>";
}
for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
for($b = 97; $b <= 122; $b++)
{
echo chr($a).chr($b)."<br/>";
}
}
for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
for($b = 97; $b <= 122; $b++)
{
for($c = 97; $c <= 122; $c++)
{
echo chr($a).chr($b).chr($c)."<br/>";
}
}
}
for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
for($b = 97; $b <= 122; $b++)
{
for($c = 97; $c <= 122; $c++)
{
for($d = 97; $d <= 122; $d++)
{
echo chr($a).chr($b).chr($c).chr($d)."<br/>";
}
}
}
}
?>
SimonMayer
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 14
Solved Threads: 10