<?php $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), '/')); preg_match_all($regex, $result, $match); switch($match[0][1]){ case '-': echo $match[0][0] - $match[0][2]; break; case '*': echo $match[0][0] * $match[0][2]; break; case '+': echo $match[0][0] + $match[0][2]; break; case '/': echo $match[0][0] / $match[0][2]; break; }
You have javascript disabled. You will not be able to edit any code.