0

Hi all. I'm attempting to output all of the possible combination from "a" to "zzzz" and I'm wondering if theres an easy way of doing that. As an example output would be ...

a
b
c
....
z
aa
ab
ac
....
az
ba
bb
...

and so on..

Is there a way to just have a binary or hex value, cast it as an ascii to get your first value, then increment it and cast it again.. and so on?

Any help would be much appreciated.

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Barefootsanders
1

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 ) . '<br />';
		$i++;
	}
	$s++;
}

That what you are talking about?

Votes + Comments
Great!
0

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 ) . '<br />';
		$i++;
	}
	$s++;
}

That what you are talking about?

Well sorta.. It would basically generate all teh combinations of a set containing 'a' through 'z'. i.e. {a,b,c,...,z}. So it owuldnt just generate aa, bb, cc.. it would go through all the possible combinations with a as the first character then b as the second character and so on.. sorry if I was not specific enough in my initial post.

0

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/>";
			}
		}
	}
}
?>
0

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/>";
			}
		}
	}
}
?>
0

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?

Thanks so much. Thats exactly what I was looking for. I am sorta unfamiliar with PHPs type casting syntax. I'm trying to stress test a script i'm preparing and just thought this would be a cool way to do it as well as a way to test myself and learn more.

Thanks again!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.