I would use preg_replace_callback :
$input = '<div style="padding:10px 15px; font-size:1em ;height:2%;"></div>'; $output = preg_replace_callback('/([0-9]+)\s*(px|em|%)/i', function($matches){ $k = 2; return ($matches[1] * $k).$matches[2]; }, $input);
The above numbers replace only with the numbers px , em or % .
You can also provide $k lambda functions yourself, for more flexibility:
$k = 2; $output = preg_replace_callback('/([0-9]+)\s*(px|em|%)/i', function($matches) use ($k){ return ($matches[1] * $k).$matches[2]; }, $input);
source share