- var_dump: documentation ( source)
- create_function: documentation ( source)
- trim: documentation ( source)
- preg_replace: documentation ( source)
<?php
function calculate_string( $mathString ) {
$mathString = trim($mathString); // trim white spaces
$mathString = preg_replace('#[^0-9\+\-\*\/\(\) ]#', '', $mathString); // remove any non-numbers chars; exception for math operators
var_dump($mathString);
$compute = create_function("", "return (" . $mathString . ");" );
return 0 + $compute();
}
$string = " 4141.58-3878.96-262.62";
echo calculate_string($string); // outputs 8