我只需要用另一个字符串
替换字符串中的前三个匹配项$strings = "Apple Orange Mango Orange Grapes Apple Orange Banana Orange Dates Orange";
应输出如下:
Apple Strawberry Mango Strawberry Grapes Apple Strawberry Banana Orange Dates Orange
我试过了
$strings = str_replace("Orange", "Strawberry", $strings);
但它会替换字符串中的所有匹配项。
答案 0 :(得分:5)
您可以使用preg_replace,
$strings = "Apple Orange Mango Orange Grapes Apple Orange Banana Orange Dates Orange";
$strings = preg_replace("/Orange/","Strawberry",$strings,3);
echo $strings;
它会按您的意愿输出