只替换一个重复的单词

时间:2014-09-03 13:29:51

标签: php replace

即使这个单词重复多次,是否可以只替换一个单词中的一个单词:

<?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。

提前致谢

2 个答案:

答案 0 :(得分:2)

答案 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;
?>