Here it is an example using the str_replace:
<?php
$variables['x'] = 60;
$variables['y'] = 30;
$variables['xb'] = 100;
$variables['yb'] = 50;
$variables['D'] = 25;
$example1 = "(x ^ 2 + y ^ 2) / 2";
$example2 = "((2 * yb) + SQR(D)) / (2 * (xb ^ 2 + yb ^ 2))";
foreach($variables as $key => $value) {
$example1 = str_replace($key, $value, $example1);
$example2 = str_replace($key, $value, $example2);
}
// Echo: (60 ^ 2 + 30 ^ 2) / 2
echo $example1;
// Echo: ((2 * 30b) + SQR(25)) / (2 * (60b ^ 2 + 30b ^ 2))
echo $example2;
?>
Ofcourse the 2nd example is wrong!
The result should echo this "((2 * 30) + SQR(25)) / (2 * (60 ^ 2 + 30 ^ 2))"
The list of ALL the math functions that should not be replaced is this "sin, cos,tan, asin, acos, SQR, ATN, atan, ABS, exp, log, ln, mod, div pi, e".
Multiply steps sounds bad, but i really wana see your thought (if you can spend your time to this).