fork download
  1. <?php
  2.  
  3. $text = "a b c";
  4. $replacement = array("a b(?! c\b)" => "ab","b c" => "bc");
  5. $search = array_map(function($v){
  6. return preg_quote($v, "/");
  7. }, array_keys($replacement));
  8.  
  9. echo $text = preg_replace_callback("/\b(" . implode("|", $search) . ")\b/", function($m)use($replacement){
  10. return $replacement[$m[1]];
  11. }, $text);
Success #stdin #stdout 0.04s 52480KB
stdin
Standard input is empty
stdout
a bc