我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

检查输入是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 would 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);

近期文章

  • 帮助正则表达式匹配
  • flash_set,PHP中的flash_get?
  • 注销代码根本不执行。 PHP
  • 删除PHP中的确认查看要删除的条目
  • 不使用mysql_insert_id()获取最后插入记录的id
  • 选择Web服务器软件从[关闭]运行PHP
  • PHP中简单正则表达式的问题
  • 在运行PHP脚本之间传递数据
  • 如何在php中为数组定义默认值以避免未定义的偏移量?
  • 在If语句中可以在Javascript中包含Php变量或URL吗?
  • PHP:我将保存php文件,我可以在浏览器中运行它
  • 使用Zend Framework,Module和Package之间有什么区别
  • 插入标记记录
  • 使用正则表达式突破锚定标记,突出显示PHP中的搜索词
  • PHP Foreach问题?
  • 如何在php文件中使用jquery?
  • 如何在javascript中正确使用引号
  • [HTML / CSS / JavaScript / PHP]:必须编写哪些订单代码?
  • 在没有框架的其他网页内显示网页
  • 如何做自动换行

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华