ctype_xdigit
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
ctype_xdigit — 检测字符是否只包含十六进制字符
参数
返回值
如果 text
中每个字符都是十六进制“数字”,也就是只能包含十进制数字和
[A-Fa-f]
的字符,那么就返回 true
。否则返回 false
。当使用空字符串调用时,结果始终为 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.
+添加备注
用户贡献的备注 1 note
tom at hgmail dot com ¶
19 years ago
This function shows its usefulness on a web site where a user is asked to entered a hexidecimal color code for a color. To prevent breaking W3C standard and having them enter in "neon-green" or the wrong type of code like 355511235.
In conjunction with strlen() you could create a function like this:
function check_valid_colorhex($colorCode) {
// If user accidentally passed along the # sign, strip it off
$colorCode = ltrim($colorCode, '#');
if (
ctype_xdigit($colorCode) &&
(strlen($colorCode) == 6 || strlen($colorCode) == 3))
return true;
else return false;
}
备份地址:http://www.lvesu.com/blog/php/function.ctype-xdigit.php