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