2011-11-28 53 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