|
Posted by Lόpher Cypher on 01/01/06 13:52
Daemon wrote:
> Hello,
>
> I am trying to create a script that will create a loop threw ascii
> characters 32 - 126, but be limited to the number in a varible,
> controlling how many returns. I figgured a control of how many array
> keys would be a good spot, say I only want strings that start with 3
> characters and end at 5 charcters.
>
> And I wanted to use an array for all possible ascii characters. So far,
> the code I have is a bit of a mess and was hoping for some one that has
> more knowledge in php to lead me in the right direction to how to
> accomplish my task at hand.
>
> <?php
....
> ?>
>
Man, that was some crazy code. :)
Let me see if I understand what you are trying to do. You have
characters with ascii codes 32..126, you want to generate all possible
strings consisting of characters with those codes, whose length is
between some minimum and maximum?
Like, if min=3 and max=5 and restricting to set 'a'..'z', it'd be
aaa, aab, aac, ..., aaz, aba, abb, ..., zzz, aaaa, aaab, ..., zzzz
Right? Then why not use recursion?
$startCode = 32;
$endCode = 126;
function gen(&$resultArray,$min,$max,$parentStr) {
global $startCode,$endCode;
if (strlen($parentStr)+1 >= $min &&
!isset($resultArray[strlen($parentStr)+1])) {
$resultArray[strlen($parentStr)+1] = array();
}
for ($i = $startCode; $i <= $endCode; $i++) {
$str = $parentStr.chr($i);
if (strlen($str) >= $min) {
array_push(&$resultArray[strlen($str)],$str);
}
if (strlen($str) < $max) {
gen(&$resultArray,$min,$max,$str);
}
}
}
function generate($min,$max) {
$resultArray = array();
gen(&$resultArray,$min,$max,"");
return $resultArray;
}
$arr = generate(3,5);
echo "<pre>";print_r($arr);echo "</pre>";
luph
Navigation:
[Reply to this message]
|