|
Posted by Daemon on 01/01/06 22:02
Lüpher Cypher wrote:
> 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
Thats incredible... exactly what I was talking about... I will have to
do some work.. so it doesnt go into an array when its generated, cause
that'll return a large arrays when you want to create large amounts of
information.
I saw a brute force script for MD5 and was just amaazed at the
complexity of the script just returning aa = md5(aa) then ab =
md5(ab)... and so on.
What I was thinking however, to skip the large returns is to place all
the characters in their own key postition in an array, much like placing
rows of data in a table to align with its appropriat column
"Age Sex Country
12 M UK
15 F USA"
So for the brute force script...
Array (known as $stringArray)
[0] => position 0 (or known as 1)
[1] => position 1 (or known as 2)
.........
And that 0 would change 1 as soon as it went through the loop
properally, and reset 0 as it soon did so.
And then just simply use:
$gen_string = $null; // Nullify the string before using.
array_reverse($stringArray) // Reverse the array, or itll look like
[0].[1].[2]...
foreach ($stringArray as $key => $string) { // Foreach will go threw the
array from first to last!!
$gen_string .= $stringArray[$key]; // I'm sure there was an eiser way
to do this, but its logical!!
}
But from this script it'll give me a better opertunity too so.
And so far for the reason I have come up with this idea... hey, its
logical, its not well known so why not build it!! We need more hackers
in the world!
Navigation:
[Reply to this message]
|