百科狗-知识改变命运!
--

ctype_digit() - ctype函数(字符类型检测)

乐乐12个月前 (11-21)阅读数 20#技术干货
文章标签字符串

ctype_digit()

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

做纯数字检测

说明

ctype_digit(string $text): bool

检查提供的string和$text里面的字符是不是都是数字。

参数

$text

ctype_digit() - ctype函数(字符类型检测)

需要被测试的字符串。

返回值

如果$text字符串是一个十进制数字,就返回TRUE;反之就返回FALSE

更新日志

版本说明
5.1.0在 PHP 5.1.0 之前,当$text是一个空字符串的时候,该函数将返回TRUE

范例

一个ctype_digit()例子

以上例程会输出:

The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.

一个通过ctype_digit()来比对字符和整数的例子。

注释

Note:

这个函数的参数要求是一个string这一点是非常有用的,因此当你传入一个integer的参数也许不能得到期望的结果。然后,同样需要注意HTML表单将会返回数字字符串而不是一个整型。看下手册的types章节。Note:

如果给出一个-128 到 255 之间(含)的整数,将会被解释为该值对应的ASCII字符(负值将加上 256 以支持扩展ASCII字符).其它整数将会被解释为该值对应的十进制字符串.

参见

  • ctype_alnum()做字母和数字字符检测
  • ctype_xdigit()检测字符串是否只包含十六进制字符
  • is_numeric()检测变量是否为数字或数字字符串
  • is_int()检测变量是否是整数
  • is_string()检测变量是否是字符串
All basic PHP functions which i tried returned unexpected results. I would just like to check whether some variable only contains numbers. For example: when i spread my script to the public i cannot require users to only use numbers as string or as integer. For those situation i wrote my own function which handles all inconveniences of other functions and which is not depending on regular expressions. Some people strongly believe that regular functions slow down your script.
The reason to write this function:
1. is_numeric() accepts values like: +0123.45e6 (but you would expect it would not)
2. is_int() does not accept HTML form fields (like: 123) because they are treated as strings (like: "123").
3. ctype_digit() excepts all numbers to be strings (like: "123") and does not validate real integers (like: 123).
4. Probably some functions would parse a boolean (like: true or false) as 0 or 1 and validate it in that manner.
My function only accepts numbers regardless whether they are in string or in integer format. 
I just wanted to clarify a flaw in the function is_digit() suggested by "info at directwebsolutions dot nl " .. 
It returns true in case of negative integers and false in case of strings that contain negative integers .
 example:
is_digit(-10); // returns ture
is_digit('-10'); // returns false
Also note that 
Interesting to note that you must pass a STRING to this function, other values won't be typecasted (I figured it would even though above explicitly says string $text).
I.E.

Will return false, even though, when typecasted to string, it would be true.

Returns True.
Could do this too:

Which will also return true, as it should.
ctype_digit() will treat all passed integers below 256 as character-codes. It returns true for 48 through 57 (ASCII '0'-'9') and false for the rest.
ctype_digit(5) -> false
ctype_digit(48) -> true
ctype_digit(255) -> false
ctype_digit(256) -> true
(Note: the PHP type must be an int; if you pass strings it works as expected)
Note that an empty string is also false:
ctype_digit("") // false
Please note that ctype_digit() will say true for strings such as '00001', which are not technically valid representations of integers, while saying false to strings such as '-1', which are. It's basically a faster version of the regex /^\d+$/. As the name says, it answers the question "does this string contain only digits" literally. It does not answer "is this a valid representation of an integer". If that's what you want, use is_int(filter_var($val, FILTER_VALIDATE_INT)) instead.
is_numeric gives true by f. ex. 1e3 or 0xf5 too. So it's not the same as ctype_digit, which just gives true when only values from 0 to 9 are entered.
Using is_numeric function is quite faster than ctype_digit.
is_numeric took 0.237 Seconds for one million runs. while ctype_digit took 0.470 Seconds.
If you need to check for integers instead of just digits you can supply your own function such as this: 
The ctype_digit can be used in a simple form to validate a field:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)