0
我尝试了一些附加到另一个号码预浸取代占位符
$n = 123;
$str = "some string with tt=789_ and more";
echo preg_replace("/tt=[0-9]+/", "$0$n", $str);
我预计这将打印“一些字符串TT = 789_123多”由于某种原因,我得到“一些字符串23_和更多“。
我尝试了一些附加到另一个号码预浸取代占位符
$n = 123;
$str = "some string with tt=789_ and more";
echo preg_replace("/tt=[0-9]+/", "$0$n", $str);
我预计这将打印“一些字符串TT = 789_123多”由于某种原因,我得到“一些字符串23_和更多“。
在您的例子的$0$n
转化为$0123
可confuse preg_replace
(见节约更换)。
所以正确的方法是做到以下几点:
$n = 123;
$str = "some string with tt=789_ and more";
echo preg_replace("/tt=[0-9_]+/", "\${0}$n", $str);
我还添加了_
你的性格类否则返回tt=789123_
。
谢谢,那工作 – georg