0

Let's say I have an array consisting of characters from these ranges "a-zA-Z0-9". What I want to do is get all possible combinations possible in a 4-letter range. Characters can be repeated in the string. I'm looking for a brute-force way to do it.

This is how I believe the iteration would be:

"aaaa" "aaab" "aaac" ... "9999"

voldomazta
  • 1,238
  • 1
  • 10
  • 18
  • Consider rephrasing your entire question so that you actually ask a question – Layke Oct 05 '11 at 17:40
  • This seems like a very strange goal to try to achieve. Maybe if we had some context we could better help you solve your problem. – Chris Oct 05 '11 at 17:40
  • Answer you need can be found [here](http://stackoverflow.com/questions/361/generate-list-of-all-possible-permutations-of-a-string) – user973254 Oct 05 '11 at 17:46
  • 1
    Do you mean "permutations" in the strict mathematical sense, or do you just want all possible three-letter words from the given alphabet? To clarify, if your alphabet is "abcde", then "aaa" is not, strictly speaking, a permutation because a permutation doesn't repeat an element. Also in math-speak, a permutation is different from a combination--order matters in a permutation, so "abc" and "cba" are different permutations, but represent the same combination. – eaj Oct 05 '11 at 17:48
  • This question suffers from the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the [overall goal](http://www.catb.org/~esr/faqs/smart-questions.html#goal)? – outis Oct 05 '11 at 18:02
  • Possible dup of: [Generate list of all possible permutations of a string](http://stackoverflow.com/questions/361/generate-list-of-all-possible-permutations-of-a-string), [Combinations, Dispositions and Permutations in PHP](http://stackoverflow.com/questions/1679605/combinations-dispositions-and-permutations-in-php), [Php recursion to get all possibilities of strings](http://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings), [How to build a character table](http://stackoverflow.com/questions/3020587/how-to-build-a-character-table) ... – outis Oct 05 '11 at 18:12

2 Answers2

0

In brute-force fashion:

$chars = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'));
$cnt = count($chars);
$strings = array();
for ($first = 0; $first < $cnt; $first++) {
   for ($second = 0; $second < $cnt; $second++) {
       for ($third= 0; $third< $cnt; $third++) {
           for ($fourth= 0; $fourth < $cnt; $fourth++) {
              $strings[] = $chars[$first] . $chars[$second] . $chars[$third] . $chars[$fourth];
           }
       }
   }
}

You'll end up with a honkin' big array, so you might want to sprinkle some database operations into one of the loops, so $strings doesn't get too big and kill your script by hitting a memory limit.

Marc B
  • 340,537
  • 37
  • 382
  • 468
0

to random access you can use base_convert:

function getHash($i)
{
  //- allowed Letters
  $letters = 'aAbBcCdDeEfFgG';
  $len = strlen($letters);

  //- convert $i to base of $letters
  $i_in_base_of_letter = base_convert($i, 10, $len);

  //- replace letters
  return strtr($i_in_base_of_letter, '0123456789abcdefghijklmnopqrstuvwxyz', $letters);

}

or manuel if your base is larger then 36 (you have more then 36 Chars)

function getHash($i)
{
  //- allowed Letters
  $letters = 'aAbBcCdDeEfFgG';
  $letters_arr = str_split($letters, 1);

  $len = strlen($letters);

  $out ='';

  if ($i>0)
  {
    while ($i>0)
    {
      $r = $i % $len;
      $out = $letters_arr[$r] . $out;
      $i = floor ($i / $len);
    }
    return $out;
  }
  else
  {
    return $letters_arr[0];
  }
}

to iterate you can use:

for($i=0; $i<100; $i++)
{
  echo getHash($i) ."\n";
}

this way is not that fast as the brute force methode.

Regards Thomas

Thomas
  • 1,765
  • 1
  • 15
  • 17