Display possible combinations of string

Set two iterators and print everything between them. So something like this:

<?
$str = "How are you";
$words = explode(" ",$str);
$num_words = count($words);
for ($i = 0; $i < $num_words; $i++) {
  for ($j = $i; $j < $num_words; $j++) {
    for ($k = $i; $k <= $j; $k++) {
       print $words[$k] . " ";
    }
    print "\n";
  }
}
?>

Output


How 
How are 
How are you 
are 
are you 
you 

Leave a Comment