2011-11-28 56 views
1

我只是想替换以下字符串:替换字符串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 

我怎么能做到这一点的更有效,更容易办法?

+2

看看这里[preg_replace_callback](http://php.net/manual/en/function.preg-replace-callback.php)。你能明白吗? –

+0

不,我不明白 – fawad

回答

2
echo preg_replace_callback("/@/", 
    function(){static $count=0; return ++$count;}, 
    "[email protected]@[email protected]"); 
+0

所有句子都存储在$ string中。我应该用$ string替换“A @ B @ C @”吗? – fawad

+0

是的,和'“/ @ /”'与你正在替换的实际模式。 – Wrikken

+0

我已经尝试过,但它不工作。 preg_replace_callback(“。:。”, function(){static $ count = 0; return ++ $ count;}, $ Product_description); – fawad