檢測字串是否只包含十六進制字元

ctype_xdigit

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

ctype_xdigit 檢測字串是否只包含十六進制字元

說明

ctype_xdigit(string $text): bool

檢查 stringtext 裡面的字元是不是都是十六進制字串。

參數

text

需要被測試的字串。

返回值

如果 text 裡面的每個字元都是十六進制字元,也就是隻能包含十進制的數值和 [A-Fa-f] 的字母,則返回 true 。否則返回 false

範例

示例 #1 一個 ctype_xdigit() 的例子

<?php
$strings 
= array('AB10BC99''AR1012''ab12bc99');
foreach (
$strings as $testcase) {
    if (
ctype_xdigit($testcase)) {
        echo 
"The string $testcase consists of all hexadecimal digits.\n";
    } else {
        echo 
"The string $testcase does not consist of all hexadecimal digits.\n";
    }
}
?>

以上例程會輸出:

The string AB10BC99 consists of all hexadecimal digits.
The string AR1012 does not consist of all hexadecimal digits.
The string ab12bc99 consists of all hexadecimal digits.

註釋

注意:

如果給出一個 -128 到 255 之間(含)的int, 將會被解釋為該值對應的ASCII字元 (負值將加上 256 以支援擴充套件ASCII字元). 其它整數將會被解釋為該值對應的十進制字串.

參見

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *