如何替换PHP只有第一个字和第二个字而不是替换第三个字?如何只替换PHP的第一个字和第二个字而不是替换第三个字?
我
$test = "hello i love animal love dog and and love tree";
我想更换最前一页和secode字love
到underscore
,而不是取代第三字love
这样。
hello i _ animal _ dog and and love tree
然后,我用这个代码
$test = str_replace('love', '_', $test);
echo $test;
但结果将是
hello i _ animal _ dog and and _ tree
如何我可以只更换第一和第二个字,而不是取代第三个单词吗?
的可能的复制(HTTP [PHP STR \ _replace()有限制PARAM?]://计算器。 com/questions/8510223/php-str-replace-with-a-limit-param) – Rulisp
@Rulisp对str_replace()没有限制参数,有一个计数参数,但允许你传入一个变量作为指针为了获得被替换的字符串的实例数量。它实际上并不允许您限制要替换的实例的数量。 – Dave