Checking input is a sum in PHP
I have a form where the user can type something and I want my script to check if it's a sum (e.g. 5 x 5 or 3+ 3) how w开发者_运维问答ould I do this? Presumably using Regular Expressions?
If you mean an "arithmetic operation", it would be something like (including floating point numbers)
preg_match('/-?\d+(\.\d+)?\h*[-+x\/]\h*-?\d+(\.\d+)?/', $input);
You are talking about checking that this is a sum, but you are writting a product! If you want to check a sum, then accept only numbers and "+" sign :
preg_match('/^[ +0-9]+$/', $expression);
精彩评论