How to generate all permutations of a string in PHP ?

Posted by Johan on Stack Overflow See other posts from Stack Overflow or by Johan
Published on 2010-04-11T12:56:26Z Indexed on 2010/04/11 13:23 UTC
Read the original article Hit count: 165

Filed under:
|

I need an algorithm that return all possible combination of all characters in one string.

I've tried:

$langd = strlen($input);
 for($i = 0;$i < $langd; $i++){
     $tempStrang = NULL;
     $tempStrang .= substr($input, $i, 1);
  for($j = $i+1, $k=0; $k < $langd; $k++, $j++){
   if($j > $langd) $j = 0;
   $tempStrang .= substr($input, $j, 1);
 }
 $myarray[] = $tempStrang;
}

But that only returns the same amount combination as the length of the string.

Say the $input is = "hey", the result would be: hey, hye, eyh, ehy, yhe, yeh.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about php