I'm trying to create function which accepts a string (contains simple math expression) then splits each of the parts as an array.
For example the input is 2 + 3 * 7
or 2 – 5 / 3.4
, the output should be like ["2", "+", "3", "*", "7"]
and ["2", "-", "5", "/", "3.4"]
Here are my codes:
$input = "2 + 3 * 7";
$input = "2-5/3.4";
function splitExpression($string) {
$result = explode (" ", $input);
print_r ($result);
}
Using only explode, of course the 1st example works nicely, but not the same with the other.