fork download
  1. <?php
  2.  
  3. $string = "fuel cap for car";
  4.  
  5. $find = array('cap', 'fuel cap for car');
  6. $replace = array('hat', 'auto parts');
  7. $arr = array_combine($find, $replace);
  8. usort($find, function($a, $b) {
  9. return strlen($b) - strlen($a);
  10. });
  11.  
  12. echo preg_replace_callback('~\b(?:' . implode('|', $find) . ')\b~', function($x) use ($arr) {
  13. return $arr[$x[0]];
  14. }, $string);
Success #stdin #stdout 0.02s 24236KB
stdin
Standard input is empty
stdout
auto parts