What if I want to multiply all numbers by 2? Can this be done with regex replace? Note the $1*2 part that obviously doesn't work. How would I do this?
$foo = "soup 12 cake 23 pants";
$bar = preg_replace('~(\d+)~', $1*2, $foo);
You could use preg_replace_callback:
preg_replace_callback('~(\d%)~', function($match) { return $match[1]*2; }, $foo);
preg_replace_callbackis what I needed. Thanks!