判断用户名和密码正则表达式
<?php $string[] = 'lewis_0913'; $string[] = '刘易斯_0913'; $string[] = 'lewis&&12'; $string[] = '^&*_123'; $string[] = '123刘易斯'; $usernamePattern = '/^[_a-zA-Z0-9\x{4e00}-\x{9fa5}]{3,16}$/u'; $passwordPattern = '/[\x{4e00}-\x{9fa5}]{3,16}/u'; foreach( $string as $element ) { preg_match( $usernamePattern , $element , $matches[] ); } foreach( $string as $element ) { preg_match( $passwordPattern , $element , $matches2[] ); } var_dump( $matches ); echo '<br>'; var_dump( $matches2 ); ?>
输出
array(5) { [0]=> array(1) { [0]=> string(10) "lewis_0913" } [1]=> array(1) { [0]=> string(14) "刘易斯_0913" } [2]=> array(0) { } [3]=> array(0) { } [4]=> array(1) { [0]=> string(12) "123刘易斯" } } array(5) { [0]=> array(0) { } [1]=> array(1) { [0]=> string(9) "刘易斯" } [2]=> array(0) { } [3]=> array(0) { } [4]=> array(1) { [0]=> string(9) "刘易斯" } }
$usernamePattern='/^[_a-zA-Z0-9\x{4e00}-\x{9fa5}]{3,16}$/u';
\x{4e00}-\x{9fa5}表示中文字
末尾的/u(PCRE_UTF8)
此修正符启用了一个PCRE中与Perl不兼容的额外功能。模式字符串被当成UTF-8。本修正符在Unix下自PHP4.1.0起可用,在win32下自PHP4.2.3起可用。
相关推荐
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03