[英]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 (eg 5 x 5 or 3+ 3) how would I do this? 我有一个表单,用户可以在其中键入一些内容,我希望我的脚本检查它是否为总和(例如5 x 5或3+ 3),我该怎么做? Presumably using Regular Expressions? 大概使用正则表达式?
如果您指的是“算术运算”,则可能是类似的东西(包括浮点数)
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.