Jump to content

try every possibility


aebstract

Recommended Posts

Simple bruteforce script

 

 

<?php

function brute ($c, $l, $p = FALSE) {

  foreach ($c as $chr) {
    $pre = $p . $chr;
    if ($l > 1)
      brute($c, $l - 1, $pre);
    else
      echo $pre . "<br>\n";
  }

}

$charset = array('a', 'b', 'c', 'd', 'e', 'f');

brute($charset, 5);

?>

 

Have fun, don't get in trouble :D

Link to comment
https://forums.phpfreaks.com/topic/94319-try-every-possibility/#findComment-483082
Share on other sites

Well I actually got what I needed and did state exactly what I was needing, just didn't say what it was for. Anyhow, with this, is there an easy way to make it group in two doubles and do something such as:

aaaaaa

aaaabb

aaaacc

bbaaaa

bbaabb

 

etc, in doubles versus each letter individually? Of course keeping those doubles the same character.

Link to comment
https://forums.phpfreaks.com/topic/94319-try-every-possibility/#findComment-483137
Share on other sites

I'll explain it modified

 

<?php

// $c is an array, with every string you want to loop through
// $l is the length of the string
// $p is a 'prefix' for fruther calls, and will be explained later

function brute ($c, $l, $p = FALSE) {
  // loop through strings
  foreach ($c as $chr) {
    // this sets up the prefix... below i will call the function again, and this lets the next call of the function know what this call has parsed
    $pre = $p . $chr;
    // this checks if this is the last string to loop through
    if ($l > 1)
      // calls itself, lowering the length by 1 each time...
      brute($c, $l - 1, $pre);
    else
      // this is the last call, echo it.
      echo $pre . "<br>\n";
  }

}

$charset = array('aa', 'bb', 'cc', 'dd', 'ee', 'ff', '00', '11', '22', '33', '44', '55', '66', '77', '88', '99');

brute($charset, 3);

?>

 

Here's what the function does in english...

 

It's called, and it begins looping through the charset...

It defines the prefix as the the current entry in the charset, in this example, 'aa'.

It checks to see how many characters are left to loop though, in this case, it's 3

Because it's 3, it calls itself again, but decreases the characters left to 2, and defines a prefix variables to 'aa'

It repeats the steps above, the prefix is now 'aaaa' and it calls itself again, characters left is now 1..

It defines the prefix as 'aaaaaa' and because it's the last character left, it does not call itself again, but instead, echo's the prefix.

The foreach loop continues for bb-99 with 'aaaa' as the prefix.

The function ends, and the previous call comes in, and sets the prefix as 'aabb', and because the characters left is now 2 again, it calls itself with 'aabb' as the prefix...

 

And it continues.

 

I don't suggest using this script every time a user wants to see every hex value... but it's great for getting them once, and building a static page that will load much faster ;)

Link to comment
https://forums.phpfreaks.com/topic/94319-try-every-possibility/#findComment-483205
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.