How to generate all permutations of a string in PHP ?
- by Johan
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.