I have php code that writes a string, which is actually an html file, to the server, but before the write I want to rip through and replace all "Npx" with "[N/10]rem". So "width:203px" would become "width:20.3rem" and top:46px" would become "top:4.6rem". Does anyone see a regex string that will do this?
Thanks
Just capture the digit before px
then match the string px
and replace all the chars with .$1rem
. Where $1
refers to the characters which are present inside the group index 1.
(\d)px
Replacement string:
.$1rem
$string = <<<EOT
width:203px
top:46px
top:6px
EOT;
$pattern = "~(\d)px~";
$replacement = ".$1rem";
echo preg_replace($pattern, $replacement, $string);
Output:
width:20.3rem
top:4.6rem
top:.6rem