©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PHP 4 >= 4.0.4, PHP 5, PHP 7)
ctype_digit — 做纯数字检测
$text
)
检查提供的 string 和 text
里面的字符是不是都是数字。
text
需要被测试的字符串。
如果 text
字符串是一个十进制数字,就返回 TRUE
;反之就返回 FALSE
。
版本 | 说明 |
---|---|
5.1.0 |
在 PHP 5.1.0 之前,当 text 是一个空字符串的时候,该函数将返回 TRUE 。
|
Example #1 一个 ctype_digit() 例子
<?php
$strings = array( '1820.20' , '10002' , 'wsl!12' );
foreach ( $strings as $testcase ) {
if ( ctype_digit ( $testcase )) {
echo "The string $testcase consists of all digits.\n" ;
} else {
echo "The string $testcase does not consist of all digits.\n" ;
}
}
?>
以上例程会输出:
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.
Example #2 一个通过 ctype_digit() 来比对字符和整数的例子。
<?php
$numeric_string = '42' ;
$integer = 42 ;
ctype_digit ( $numeric_string ); // true
ctype_digit ( $integer ); // false (ASCII 42 is the * character)
is_numeric ( $numeric_string ); // true
is_numeric ( $integer ); // true
?>
Note:
这个函数的参数要求是一个 string 这一点是非常有用的,因此当你传入一个 integer 的参数也许不能得到期望的结果。然后,同样需要注意HTML表单将会返回数字字符串而不是一个整型。 看下手册的 types 章节。
Note:
如果给出一个 -128 到 255 之间(含)的整数, 将会被解释为该值对应的ASCII字符 (负值将加上 256 以支持扩展ASCII字符). 其它整数将会被解释为该值对应的十进制字符串.
[#1] smicheal2 at gmail dot com [2015-10-07 13:21:02]
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.
[#2] error17191 at gmail dot com [2015-10-03 01:41:49]
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
[#3] rlerne at gmail dot com [2013-04-25 18:04:18]
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.
<?PHP
$val = 42; //Answer to life
$x = ctype_digit($val);
?>
Will return false, even though, when typecasted to string, it would be true.
<?PHP
$val = '42';
$x = ctype_digit($val);
?>
Returns True.
Could do this too:
<?PHP
$val = 42;
$x = ctype_digit((string) $val);
?>
Which will also return true, as it should.
[#4] info at directwebsolutions dot nl [2012-05-19 21:16:19]
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.
<?php
function is_digit($digit) {
if(is_int($digit)) {
return true;
} elseif(is_string($digit)) {
return ctype_digit($digit);
} else {
// booleans, floats and others
return false;
}
}
?>
[#5] Skippy [2011-10-25 01:44:38]
If you need to check for integers instead of just digits you can supply your own function such as this:
<?php
function ctype_int($text)
{
return preg_match('/^-?[0-9]+$/', (string)$text) ? true : false;
}
?>
[#6] mdsky at web dot de [2010-05-21 12:44:35]
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.
[#7] John Saman [2010-02-05 05:38:17]
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.
[#8] strrev xc tod noxeh ta ellij [2009-12-14 08:41:41]
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)
[#9] Peter de Pijd [2009-07-18 03:53:22]
Note that an empty string is also false:
ctype_digit("") // false
[#10] a_p_leeming at hotmail dot com [2009-05-24 18:17:44]
Also note that
<?php ctype_digit("-1"); //false ?>
[#11] raul dot 3k at gmail dot com [2009-04-09 08:21:38]
The ctype_digit can be used in a simple form to validate a field:
<?php
$field = $_POST["field"];
if(!ctype_digit($field)){
echo "It's not a digit";
}
?>
Note:
Digits is 0-9