| | |
Itterating - "a" through "zzzz"
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
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.
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.
This is how I would do it:
That what you are talking about?
PHP Syntax (Toggle Plain Text)
$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?
Last edited by kkeith29; Sep 2nd, 2009 at 2:00 am.
Google is your friend.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
•
•
•
•
This is how I would do it:
PHP Syntax (Toggle Plain Text)
$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.
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 10
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?
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?
php Syntax (Toggle Plain Text)
<? 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/>"; } } } } ?>
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
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:
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:
php Syntax (Toggle Plain Text)
<? 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/>"; } } } } ?>
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
•
•
•
•
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 again!
![]() |
Similar Threads
- ";:<script><html>< // >@@#"; (PHP)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- "Save Target As.." isn't working in IE6 (Web Browsers)
- Windows 2000 Adv Server and "Printing Subsystem" (Windows NT / 2000 / XP)
- Should replacing a 15" laptop screen with 14.1" make a difference? (Monitors, Displays and Video Cards)
Other Threads in the PHP Forum
- Previous Thread: updating multiple rows with one form
- Next Thread: Freak include issue!
| Thread Tools | Search this Thread |
apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code cron curl database dataentry date display duplicates dynamic ebooks echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google href htaccess html image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching mediawiki menu mlm multiple mysql number oop paypal pdf php phpincludeissue problem query radio random recursion recursive remote script search server sessions shot sms source sp space speed sql subdomain subscription syntax system table tag tutorial tutorials update upload url validator variable vbulletin video web white youtube






