fork(1) download
  1. <?php
  2.  
  3. $string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ipsum lorem dolor Curabitur ac risus nunc. Dolor ipsum lorem.';
  4.  
  5. $lower_string = strtolower($string);
  6.  
  7. $text = array('lorem', 'ipsum', 'dolor');
  8.  
  9. $perms = AllPermutations($text);
  10. $result = 0;
  11. foreach ($perms as $piece) {
  12. $phrase = join(' ', $piece);
  13. $result += substr_count($lower_string, $phrase);
  14. }
  15.  
  16. function AllPermutations($InArray, $InProcessedArray = array())
  17. {
  18. $ReturnArray = array();
  19. foreach($InArray as $Key=>$value)
  20. {
  21. $CopyArray = $InProcessedArray;
  22. $CopyArray[$Key] = $value;
  23. $TempArray = array_diff_key($InArray, $CopyArray);
  24. if (count($TempArray) == 0)
  25. {
  26. $ReturnArray[] = $CopyArray;
  27. }
  28. else
  29. {
  30. $ReturnArray = array_merge($ReturnArray, AllPermutations($TempArray, $CopyArray));
  31. }
  32. }
  33. return $ReturnArray;
  34. }
  35.  
  36. echo $result;
  37.  
  38.  
  39. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
3