Is there a way to dynamically replace with a function similar to how .replace works in JavaScript? Comparable use case: I have a string with numbers, and I would like to pass numbers through a function:
"1 2 3" => "2 4 6" (x2)
The current preg_replace function does not seem to support the function for the argument.
For reference, in JavaScript it can be implemented as follows:
var str = "1 2 3"; str.replace(/\d+/g, function(num){ return +num * 2; });
source share