即使这个单词重复多次,是否可以只替换一个单词中的一个单词:
<?php
$sentence 'Hello my name is Abu Rayane, my age is 99 yearsl old, and my school level is -1';
$new_sentence = str_replace('my', '__', $sentence);
echo $new_sentence;
?>
我想替换第一个&#39; my&#39; ONLY。
提前致谢
答案 0 :(得分:2)
使用此http://php.net/manual/en/function.preg-replace.php,带$ limit参数。
答案 1 :(得分:1)
AlexDeb是对的。
以下是一个例子:
<?php
$sentence = 'Hello my name is Abu Rayane, my age is 99 yearsl old, and my school level is -1';
$new_sentence = preg_replace('/my/', '__', $sentence, 1);
echo $new_sentence;
?>