替换字符串PHP中多次出现的单词

替换字符串PHP中多次出现的单词,php,Php,我只想替换以下字符串: This is my first sentence. .:. This is my second sentence. .:. This is my third sentence. .:. 输出应如下所示: This is my first sentence. 1 This is my second sentence. 2 This is my third sentence. 3 我如何才能更高效、更简单地完成这项工作?请看这里。你能理解吗?所有的句子都存储在$stri

我只想替换以下字符串:

This is my first sentence.
.:.
This is my second sentence.
.:.
This is my third sentence.
.:.
输出应如下所示:

This is my first sentence.
1
This is my second sentence.
2
This is my third sentence.
3

我如何才能更高效、更简单地完成这项工作?

请看这里。你能理解吗?所有的句子都存储在$string中。我应该替换吗?”A@B@带$string的C@?是的,并且带有您正在替换的实际模式的
“/@/”
。我已经尝试过了,但它不起作用。preg_replace_回调(“.:.”,function(){static$count=0;return++$count;},$Product_description);这将是一个以
作为分隔符的模式。。。。并匹配所有文字的
。。。。我怀疑那是你想要的。如果您在创建适当的正则表达式时遇到困难,我建议您将其作为一个新问题来提出。
echo preg_replace_callback("/@/",
    function(){static $count=0; return ++$count;},
    "A@B@C@");