I am trying to create a script that cleans a string from anything that is not a number or operator and then performs the calculation.
It works fine if, for example, the sting is How much is 25 + 35 then * 8 and then / 2
, but if the string is How much is 25.5 + 35.5 then * 8 and then / 2
, the result is wrong, as it does not consider the float in the numbers
I have tried using is_float
in the for
loop but without success.
here is a demo http://sandbox.onlinephpfunctions.com/code/c06295bbb567b667cfd65ff0d736330fea0e774b
Any idea what can I do to make it calculate them right?
$result = "How much is 25.5 + 35.5";
$allowed = array('1','2','3','4','5','6','7', '8', '9','0','-','+','/','*','.');
$regex = sprintf('/[^%s]/u', preg_quote(join($allowed), '/'));
$result = preg_replace($regex, '', $result);
$str = preg_replace('/\s+/', '', $result);
//Create calculation
$number = array();
$z = 0;
for ($i = 0; $i < iconv_strlen($str); $i++) {
if (is_numeric($str[$i])) {
$number[$z] .= $str[$i];
} else {
$z++;
$number[$z] = $str[$i];
$z++;
}
};
for ($i = 0; $i < count($number); $i++) {
$number[$i] = (int) $number[$i];
$i++;
$number[$i] = (string) $number[$i];
}
$res = $number[0];
for ($i = 0; $i < count($number); $i++) {
if ($number[$i+1] === '+') {
$res += $number[$i+2];
}elseif($number[$i+1] === '-'){
$res -= $number[$i+2];
}elseif($number[$i+1] === '*'){
$res *= $number[$i+2];
}elseif($number[$i+1] === '/'){
$res /= $number[$i+2];
}
$i++;
}
echo round($res,2);