fork download
  1. <?php
  2. //the items with replacement values.
  3. /**$items = array(
  4.   "vehical" => array('vehicle'),
  5.   "recovary" => array('recovery', 'recover', 'revary')
  6. );*/
  7.  
  8. //the items with replacement values.
  9. $items = array(
  10. "vehical" => array('vehicle', 'vehiclesy', 'whatever'),
  11. "recovary" => array('recovery', 'recover', 'revary'),
  12. "cabs" => array('cups', 'cips'),
  13. "from" => array(1, 2)
  14. );
  15.  
  16. //init the initial sentence and the array with all solutions.
  17. $sentence = 'i want to recovary my vehical from the cabs';
  18. $solution = [];
  19.  
  20. //run through all keywords to execute the replacements.
  21. foreach ($items as $item => $value) {
  22. if (count($value) > 0) {
  23. if (count($solution) > 0) {
  24. $solution = getReplacements($solution, $item, $value);
  25. } else {
  26. $solution = getReplacements($sentence, $item, $value);
  27. }
  28. }
  29. }
  30.  
  31. //output the solutions.
  32. array_walk_recursive($solution, 'output');
  33.  
  34. function output(&$item,$key) {
  35. echo $item."\n";
  36. }
  37.  
  38. /**
  39.  * Function to execute the replacements.
  40.  * @param array|string $sentence An array or string on which the replacements should execute.
  41.  * @param string $item The word which will be replaced.
  42.  * @param array $values The replacement values for the item.
  43.  * @return array An array with all solutions of this function.
  44.  */
  45. function getReplacements($sentence, $item, $values)
  46. {
  47. $solutions = [];
  48.  
  49. foreach ($values as $value) {
  50. $sol = str_replace($item, $value, $sentence);
  51.  
  52. if (is_array($sol)) {
  53. $solutions = array_merge($solutions, $sol);
  54. } else {
  55. $solutions[] = $sol;
  56. }
  57. }
  58.  
  59. return $solutions;
  60. }
Success #stdin #stdout 0.03s 52432KB
stdin
Standard input is empty
stdout
i want to recovery my vehicle 1 the cups
i want to recovery my vehiclesy 1 the cups
i want to recovery my whatever 1 the cups
i want to recover my vehicle 1 the cups
i want to recover my vehiclesy 1 the cups
i want to recover my whatever 1 the cups
i want to revary my vehicle 1 the cups
i want to revary my vehiclesy 1 the cups
i want to revary my whatever 1 the cups
i want to recovery my vehicle 1 the cips
i want to recovery my vehiclesy 1 the cips
i want to recovery my whatever 1 the cips
i want to recover my vehicle 1 the cips
i want to recover my vehiclesy 1 the cips
i want to recover my whatever 1 the cips
i want to revary my vehicle 1 the cips
i want to revary my vehiclesy 1 the cips
i want to revary my whatever 1 the cips
i want to recovery my vehicle 2 the cups
i want to recovery my vehiclesy 2 the cups
i want to recovery my whatever 2 the cups
i want to recover my vehicle 2 the cups
i want to recover my vehiclesy 2 the cups
i want to recover my whatever 2 the cups
i want to revary my vehicle 2 the cups
i want to revary my vehiclesy 2 the cups
i want to revary my whatever 2 the cups
i want to recovery my vehicle 2 the cips
i want to recovery my vehiclesy 2 the cips
i want to recovery my whatever 2 the cips
i want to recover my vehicle 2 the cips
i want to recover my vehiclesy 2 the cips
i want to recover my whatever 2 the cips
i want to revary my vehicle 2 the cips
i want to revary my vehiclesy 2 the cips
i want to revary my whatever 2 the cips