且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查变量是否有数字 php

更新时间:2022-04-10 07:28:29

您可以使用 strcspn 函数:

You can use the strcspn function:

if (strcspn($_REQUEST['q'], '0123456789') != strlen($_REQUEST['q']))
  echo "true";
else
  echo "false";

strcspn 返回不包含任何整数的部分的长度.我们将其与字符串长度进行比较,如果它们不同,那么一定是一个整数.

strcspn returns the length of the part that does not contain any integers. We compare that with the string length, and if they differ, then there must have been an integer.

无需为此调用正则表达式引擎.

There is no need to invoke the regular expression engine for this.