ctype_alnum() 函数检测字符串是否全部为字母和(或)数字字符。
ctype_alnum ( $text );
它检查提供的字符串,文本中的所有字符是否都是字母和(或)数字。
在标准C语言环境中,字母仅为[A-Za-z],该函数等效于preg_match('/^[a-z0-9]+$/iD',$ text)。
| 序号 | 参数和说明 |
|---|---|
| 1 | text(必需) 要检测的字符串。 |
如果文本中的每个字符都是字母或数字,则返回TRUE,否则返回FALSE。
遍历检测数组元素,是否全为字母数字
<?php
$strings = array('hello', 'foo!#$bar');
foreach ($strings as $example) {
if (ctype_alnum($example)) {
echo "$example 由字母或数字组成。\n";
}else {
echo "$example 不全是字母或数字。\n";
}
}
?>测试看看‹/›输出结果:
hello 由字母或数字组成。 foo!#$bar 不全是字母或数字。