Skip to content

Instantly share code, notes, and snippets.

@gabriel-felipe
Last active July 3, 2018 22:25
Show Gist options
  • Save gabriel-felipe/af31fbc7ed89432a942ada30727454ec to your computer and use it in GitHub Desktop.
Save gabriel-felipe/af31fbc7ed89432a942ada30727454ec to your computer and use it in GitHub Desktop.
Regex Replace PHP Operations by BCMath functions
YOU SHOULD NOT BLINDLY REPLACE USING THESE REGEX, FALSE POSITIVES ARE POSSIBLE.
These were written to make the job easier, but it still needs checking.
Adding var regex, replaces $var1 + $var2 by bcadd($Var1, $var2, 2)
Regex: (\$[\$\'\'\"\"\[\]\_A-z0-9]+?)[\s]*\+[\s]*([\$\'\'\"\"\[\]\_A-z0-9]+)
Replace: bcadd($1,$2)
Replaces $var1 += $var2 by $var1 = bcadd($var1,$var2)
Regex: (\$[\$\'\'\"\"\[\]\_A-z0-9]+?)[\s]*\+\=[\s]*(\$[\$\'\'\"\"\[\]\_A-z0-9]+)
Replace: $1 = bcadd($1,$2)
Same as the previous one, but includes function calls and needs to have a ; at the end, so it would replace for example:
$var1 += round($var2, 2); by $var1 = bcadd($var1,round($var2, 2);
Regex: ([^\s]+)[\s]*\+\=[\s]*([^\s]+);
Replace: $1 = bcadd($1,$2);
Subtracting var regex, replaces $var1 - $var2 by bcsub($var1, $var2)
Regex: (\$[\$\'\'\"\"\[\]\_A-z0-9]+?)[\s]*\-[\s]*([\$\'\'\"\"\[\]\_A-z0-9]+)
Replace: bcsub($1,$2);
Regex: (\$[\$\'\'\"\"\[\]\_A-z0-9]+?)[\s]*\-\=[\s]*(\$[\$\'\'\"\"\[\]\_A-z0-9]+)
Replace: $1 = bcsub($1,$2)
Same as the previous one, but includes function calls and needs to have a ; at the end, so it would replace for example:
$var1 -= round($var2, 2); by $var1 = bcadd($var1,round($var2, 2);
Regex: ([^\s]+)[\s]*\-\=[\s]*([^\s]+);
Replace: $1 = bcsub($1,$2);
Multiplying var regex, replaces $var1 * $var2 by bcmul($var1, $var2)
Regex: (\$[\$\'\'\"\"\[\]\_A-z0-9]+?)[\s]*\*[\s]*([\$\'\'\"\"\[\]\_A-z0-9]+)
Replace: bcmul($1,$2)
Dividing var regex, replaces $var1 / $var2 by bcdiv($var1, $var2)
Regex: (\$[\$\'\'\"\"\[\]\_A-z0-9]+?)[\s]*\\[\s]*([\$\'\'\"\"\[\]\_A-z0-9]+)
Replace: bcdiv($1,$2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment