注册用户名称的PHP验证以及含中英文混合的字符串长度的判断

function check_name( $playerName ) {
$temp_len = ( strlen ( $playerName ) + mb_strlen ( $playerName , ' utf-8 ' )) / 2 ;
if ( $temp_len < 1 || $temp_len > 10 ) {
return ' error_len ' ;
}
else {
$reg = ' /^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$/u ' ; // 匹配中文字符,数字,字母的正则表达式
if ( preg_match ( $reg , $playerName )) {
return ' match ' ;
}
else {
return ' no_match ' ;
}
}

}


注册用户名称的验证(要求检测一输入的用户名,用户名格式要求为汉字、字母、数字的组合,用户名长度为1到10个字符):

这个地方有个问题就是用户名可以为汉字和字母或数字的组合,

如果汉字在PHP中的UTF-8或者GB2312编码下用str_len()函数会返回3,

如果利用采用UTF-8编码下利用mb_strlen()函数一个汉字将返回3,但采用gb2312编码利用mb_strlen()函数一个汉字将返回2。


你可能感兴趣的:(php开发)