I’m trying to append a 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$nis transformed to$0123which can confusepreg_replace(see the section about replacement).So the correct way is to do the following:
I’ve also added
_to your character class otherwise it returnstt=789123_.