2011-10-12 86 views
0

我嘗試了一些附加到另一個號碼預浸取代佔位符

$n = 123; 
$str = "some string with tt=789_ and more"; 
echo preg_replace("/tt=[0-9]+/", "$0$n", $str); 

我預計這將打印「一些字符串TT = 789_123多」由於某種原因,我得到「一些字符串23_和更多「。

回答

4

在您的例子的$0$n轉化爲$0123confuse preg_replace(見節約更換)。

所以正確的方法是做到以下幾點:

$n = 123; 
$str = "some string with tt=789_ and more"; 
echo preg_replace("/tt=[0-9_]+/", "\${0}$n", $str); 

我還添加了_你的性格類否則返回tt=789123_

+0

謝謝,那工作 – georg