$needle = array("Arr","Arr:","Arrang");
$haystack = " Arr is the proper way of Arr.";
echo str_replace($needle, "Arrangement", $haystack);

印刷品:Arrangement is the proper way of Arrangement
想要:Arrangement is the proper way of Arr.

最佳答案

preg_replaceimplode和分隔符一起用于数组。分隔符\s|用作数组创建的regex模式的or语句:

$needle = array("Arr","Arr:","Arrang");
$haystack = "Arr: is the proper way of Arr.";
echo preg_replace("/".implode("\s|",$needle)."\s/", "Arrangement ", $haystack, 1);

结果:
Arrangement is the proper way of Arr.

08-27 03:11