-2

I'm trying to generate all possible combinations of a string using PHP. I've searched this site for a while trying to find the right algorithm but I can't seem to find the correct one. To be more specific if i input the string "hi", I want it to return "hi", "ih", "h", "i". But I don't want it to reuse the same characters multiple times. So I wouldn't want "hh" and "ii". Is there an algorithm for this? Thanks!

Njinx
  • 69
  • 2
  • 11

1 Answers1

1

Dont take this for production:

$string = implode('',array_unique(str_split('helpa')));
$i=0;
while($i++<50000){
   $coll[substr(str_shuffle($string),0,mt_rand(1,strlen($string)))]=true;
}
ksort($coll);
print '<pre>';
print_r(array_keys($coll));

Here you can test what you want to get.

In 10000 iteration i was getting 325 combinations from a 5 length string (what seems all possible combinations)

JustOnUnderMillions
  • 3,741
  • 9
  • 12