3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "Red Cell is very good. Condition is new. But nobody buys it."; $words = ["Red Cell", "Condition", "no", "Red", "new"]; $translations = ["Red Cell", "Stav", "ne", "Červený", "nový"]; $pairs = array_combine($words, $translations); krsort($pairs); $pattern = '~\b(?:' . implode('|', array_keys($pairs)) . ')\b~u'; $result = preg_replace_callback($pattern, function ($m) use ($pairs) { return $pairs[$m[0]]; }, $string); echo $result;

preferences:
31.49 ms | 407 KiB | 5 Q