I want to replace any number bigger than X with X in a string if it matches a pattern.
Example with X = 4000 and matching function(?)
I have a string like this:
$code = "function(4500),function(3900),function2(12345)";
I want a string like this:
$code = "function(4000),function(3900),function2(12345)";
I have tried this but it doesn't work:
preg_replace('function\(\d+>4000\)', '4000', $code)
You can use the function preg_replace_callback()
here:
$str = 'String "function(4500),function(3900),function(12345)"';
$x = 4000;
$str = preg_replace_callback('/[0-9]+/', function($match) use($x) {
return min($match[0], $x);
}, $str);
echo $str;
Output:
String "function(4000),function(3900),function(4000)"
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.