检查变量是否有数字 php [英] Check if variable has a number php

查看:42
本文介绍了检查变量是否有数字 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个变量中是否有一个数字,我只想看看是否有一个我不在乎它是否有任何其他内容,例如:

I want to check if a variable has a number in it, I just want to see if there is one I don't care if it has any thing else in it like so:

"abc" - false
"!./#()" - false
"!./#()abc" - false
"123" - true
"abc123" - true
"!./#()123" - true
"abc !./#() 123" -true

如果您想知道所有数字但不是只有一个数字,则有一些简单的方法可以做到这一点.感谢您的帮助.

There are easy ways of doing this if you want to know that is all numbers but not if it just has one. Thanks for your help.

推荐答案

您可以使用 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.

这篇关于检查变量是否有数字 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆