2011-10-12 83 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