PHP Conference Japan 2024

ctype_digit

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

ctype_digit檢查是否為數字字元

說明

ctype_digit(mixed $text): bool

檢查提供的字串 text 中的所有字元是否為數字。

參數

text

被測試的字串。

注意事項:

如果提供 -128 到 255 之間(含)的 整數 (int),它會被解讀為單個字元的 ASCII 值(負值會加上 256,以便允許擴展 ASCII 範圍內的字元)。任何其他整數都會被解讀為包含該整數十進位數字的字串。

警告

從 PHP 8.1.0 開始,傳遞非字串的參數已被棄用。未來,該參數將被解讀為字串,而不是 ASCII 字碼點。根據預期的行為,應該將參數強制轉換為 字串 (string) 或明確呼叫 chr() 函式。

返回值

如果字串 text 中的每個字元都是十進位數字,則返回 true,否則返回 false。當使用空字串呼叫時,結果將始終為 false

範例

範例 #1 ctype_digit() 範例

<?php
$strings
= array('1820.20', '10002', 'wsl!12');
foreach (
$strings as $testcase) {
if (
ctype_digit($testcase)) {
echo
"字串 $testcase 由全數字組成。\n";
} else {
echo
"字串 $testcase 並非由全數字組成。\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.

範例 #2 ctype_digit() 範例,比較字串與整數

<?php

$numeric_string
= '42';
$integer = 42;

ctype_digit($numeric_string); // true
ctype_digit($integer); // false (ASCII 42 是 * 字元)

is_numeric($numeric_string); // true
is_numeric($integer); // true
?>

參見

新增筆記

使用者貢獻的筆記 13 則筆記

info at directwebsolutions dot nl
12 年前
所有我嘗試過的基本 PHP 函式都返回了非預期的結果。我只想檢查某些變數是否只包含數字。例如:當我將我的腳本公開發佈時,我不能要求使用者只能使用數字作為字串或整數。針對這些情況,我編寫了自己的函式,它可以處理其他函式的所有不便之處,並且不依賴於正規表示式。有些人堅信正規函式會降低腳本的執行速度。

編寫此函式的原因
1. is_numeric() 接受像 +0123.45e6 這樣的值(但您預期它不會)。
2. is_int() 不接受 HTML 表單欄位(例如:123),因為它們被視為字串(例如:"123")。
3. ctype_digit() 期望所有數字都是字串(例如:"123"),並且不驗證真正的整數(例如:123)。
4. 可能某些函式會將布林值(例如:true 或 false)解析為 0 或 1,並以這種方式驗證它。

我的函式只接受數字,無論它們是字串格式還是整數格式。
<?php
/**
* 檢查輸入是否僅由數字組成
* @author Tim Boormans <info@directwebsolutions.nl>
* @param $digit
* @return bool
*/
function is_digit($digit) {
if(
is_int($digit)) {
return
true;
} elseif(
is_string($digit)) {
return
ctype_digit($digit);
} else {
// 布林值、浮點數和其他
return false;
}
}
?>
smicheal2 at gmail dot com
9 年前
請注意,ctype_digit() 會對諸如 '00001' 之類的字串返回 true,這些字串在技術上不是有效的整數表示形式,而對諸如 '-1' 之類的字串返回 false,儘管它們是有效的。它基本上是正規表示式 /^\d+$/ 的一個更快版本。顧名思義,它回答的是「這個字串是否只包含數字」這個字面上的問題。它沒有回答「這是否是整數的有效表示形式」。如果您想要這樣做,請改用 is_int(filter_var($val, FILTER_VALIDATE_INT))。
Peter de Pijd
15 年前
請注意,空字串也為 false
ctype_digit("") // false
error17191 at gmail dot com
9 年前
我只是想澄清 "info at directwebsolutions dot nl" 建議的函式 is_digit() 中的一個缺陷。
它在負整數的情況下返回 true,而在包含負整數的字串的情況下返回 false。
範例
is_digit(-10); // 返回 true
is_digit('-10'); // 返回 false
rlerne at gmail dot com
11 年前
值得注意的是,您必須傳遞字串給這個函式,其他值不會被強制轉型(即使上面明確說明是 string $text,我原本以為會)。

例如:

<?PHP
$val
= 42; //生命的答案
$x = ctype_digit($val);
?>

即使在強制轉型為字串後會是 true,這仍會返回 false。

<?PHP
$val
= '42';
$x = ctype_digit($val);
?>

返回 True。

也可以這樣做

<?PHP
$val
= 42;
$x = ctype_digit((string) $val);
?>

這也會返回 true,如同預期。
strrev xc tod noxeh ta ellij
14 年前
ctype_digit() 會將所有傳入的小於 256 的整數視為字元碼。 48 到 57 (ASCII '0'-'9') 會返回 true,其餘則返回 false。

ctype_digit(5) -> false
ctype_digit(48) -> true
ctype_digit(255) -> false
ctype_digit(256) -> true

(注意:PHP 類型必須是 int;如果您傳遞字串,則會按預期工作)
a_p_leeming at hotmail dot com
15 年前
另請注意

<?php ctype_digit("-1"); //false ?>
mdsky at web dot de
14 年前
is_numeric 對於例如 1e3 或 0xf5 也會返回 true。 因此它與 ctype_digit 不同,ctype_digit 僅在輸入的值為 0 到 9 時才返回 true。
John Saman
14 年前
使用 is_numeric 函式比 ctype_digit 快很多。

is_numeric 執行一百萬次花費 0.237 秒。 而 ctype_digit 花費 0.470 秒。
zorrosNOSPAMwordsman at NOSPAM dot gmail dot com
8 年前
如果您想驗證變數是否僅包含數字,您可以將其強制轉型為字串,然後再轉回 int,並查看結果是否相同。像這樣

<?php

// 如果僅包含數字,則為 (bool) TRUE,否則為 FALSE
$isOnlyDigits = (string) (int) $input === (string) $input;

?>

我沒有進行基準測試,但我猜它比正規表達式快得多。
raul dot 3k at gmail dot com
15 年前
ctype_digit 可以用簡單的形式來驗證欄位
<?php
$field
= $_POST["field"];
if(!
ctype_digit($field)){
echo
"這不是數字";
}
?>

注意事項
數字是 0-9
brcontainer at yahoo dot com dot br
6 年前
ctype_digit 函數不支援字串中的負值

<?php
var_dump
( ctype_digit('-10') ); // 回傳 bool(false)
?>

改良(並簡化)Tim Boormans 的程式碼

<?php
/**
* 檢查輸入是否僅包含數字
* @author Guilherme Nascimento <brcontainer@yahoo.com.br>
* @param $digit
* @return bool
*/
function is_digit($digit)
{
return
preg_match('#^-?\d+$#', $digit) && is_int((int) $digit);
}
divinity76 at gmail dot com
3 年前
如果您想檢查它是否為有效的整數,可以使用以下替代方案

<?php
if(false!==filter_var($v, FILTER_VALIDATE_INT)){
// 是一個有效的整數!
$v=(int)$v;
}else{
// 不是一個有效的整數
}
?>

或者,如果您想檢查它是否為正整數(>= 0)
<?php
if(false!==filter_var($v, FILTER_VALIDATE_INT, ["options"=>["min_range"=>0]])){
// 是一個有效的正整數!
$v = (int)$v;
}else{
// 不是一個有效的正整數
}

?>
To Top