preg_replace把捕获的数字不起作用

I think I have made a bad mistake...

It's the input:

http://www.example.com/do.php?action=5&say=text
http://www.example.com/do.php?action=8&say=text
http://www.example.com/do.php?action=10&say=text

I want to put another parameter after action:

preg_replace('/action=[0-9]/', 'action=$1&param=test', $text);

but its output is:

http://www.example.com/do.php?action=&param=test&say=text
http://www.example.com/do.php?action=&param=test&say=text
http://www.example.com/do.php?action=&param=test&say=text

as you see, the action values have removed.

Where's my mistake?

The $1 is a substring match, and starts with the first parenthesis. So to use it would be /actions=([0-9]+)/ to capture the digits.

You can try

$url = "http://www.example.com/do.php?action=10&say=tex" ;
$url = preg_replace('/action=(\d+)/', "action=$1&param=test", $url);
echo $url ;

Output

http://www.example.com/do.php?action=10&param=test&say=tex