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

时间:2011-11-28 16:17:08

标签: 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

我怎样才能更有效,更轻松地完成这项工作?

1 个答案:

答案 0 :(得分:2)

echo preg_replace_callback("/@/",
    function(){static $count=0; return ++$count;},
    "A@B@C@");