I have this code, is working in all the tester I'm using for regex, but later, in my real php code it doesn't work. What I want is to replace the number in the link for something else
$value='/something.html?helperid=252';
//patern
$patternHelperId='/(?<=helperid=)\d{1,}/';
//replace
preg_replace($patternHelperId, "mynewreplacement", $value);
//debug
echo "\n$value\n";// /something.html?helperid=252????? aggain???
What's wrong??
You should assign the result of preg_replace
back to $value
, like this:
$value = preg_replace($patternHelperId, "mynewreplacement", $value);
And, as a sidenote, \\d{1,}
can be replaced with \\d+
.
preg_replace
returns the result. It does not modify the variable in-place
You have forgotten to take the result of the preg_replace function:
$newValue = preg_replace($patternHelperId, "mynewreplacement", $value);
echo "\n$newvalue\n";
A better pattern:
$patternHelperId='/helperid=\K\d++/';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.