preg replace placeholders
I'm trying to append a 开发者_StackOverflow社区number to another number
$n = 123;
$str = "some string with tt=789_ and more";
echo preg_replace("/tt=[0-9]+/", "$0$n", $str);
I expect this to print "some string with tt=789_123 and more" for some reason i'm getting "some string with 23_ and more".
In your example the $0$n
is transformed to $0123
which can confuse preg_replace
(see the section about replacement).
So the correct way is to do the following:
$n = 123;
$str = "some string with tt=789_ and more";
echo preg_replace("/tt=[0-9_]+/", "\${0}$n", $str);
I've also added _
to your character class otherwise it returns tt=789123_
.
精彩评论