$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_replace
与implode
和分隔符一起用于数组。分隔符\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.