PHP中的ctype_xdigit()函数用于检查字符串/文本的每个字符是否为十六进制数字。如果所有字符均为十六进制,则返回TRUE,否则返回FALSE。
用法:
ctype_xdigit(string text)
使用的参数:
text :这是必填参数,用于指定测试的字符串。
返回值:
如果字符串的所有字符均为十六进制,则返回TRUE,否则返回FALSE。
例子:
Input : ABCDEF0123
Output : Yes
Input : GFG2018
Output : No
Note : It checking decimal digit or a character from [A-F, a-f].
以下示例程序旨在说明ctype_xdigit()函数。
程序:1
// PHP program to check given string is
// Hexadecimal character or not
$string = 'ABab012';
// Checking above string by using
// of ctype_xdigit() function.
if ( ctype_xdigit($string)) {
// if true then return Yes
echo "Yes \n";
} else {
// if False then return No
echo "No \n";
}
?>
输出:
Yes
程序:2
再举一个ctype_xdigit()函数示例,通过使用输入作为字符串数组来检查输入包含大写小写字母和符号字符时的工作方式。
// PHP program to check given string is
//Hexadecimal character or not
$strings = array(
'ABCDEF',
'abcdef',
'0123456789X',
'0123456789',
'Gg@(*&)',
'GFG'
);
// Checking above given strings
//by used of ctype_xdigit() function .
foreach ($strings as $test) {
if (ctype_xdigit($test)) {
echo "Yes \n";
} else {
echo "No \n";
}
}
?>
输出:
Yes
Yes
No
Yes
No
No