PHP Ctype函数

概述

ctype扩展在PHP4.2.0版本后就默认开启了,此扩展提供了一系列检测字符、字符串是否符合某种规则的函数,在一些场景写使用特别方便。

常用函数列表

ctype_alnum

检测参数是否仅包含字母或数字

1
bool ctype_alnum ( string $text )

可以用在检测用户名的地方,如果需要允许使用下划线_和短横线-等字符,可先用str_replace进行替换后再检测:

1
2
3
if(!ctype_alnum(str_replace(['_', '-'], '', $username))) { 
echo 'invalid.';
}

ctype_alpha

检测参数是否仅包含字母,在标准的locale下,字母指[a-zA-Z]。

1
bool ctype_alpha ( string $text )

ctype_digit

检测参数是否仅包含数字(10进制)字符[0-9]

1
bool ctype_digit ( string $text )

ctype_lower、ctype_upper

分别检测参数中是否仅包含小写字母或者大写字母

1
2
bool ctype_lower ( string $text )
bool ctype_upper ( string $text )

ctype_xdigit

检测参数是否仅包含十六进制字符[0-9a-fA-F]

1
bool ctype_xdigit ( string $text )