只允许汉字,字母,数字和下横线组合的正则
if(isset($_GET["username"])){ $username=$_GET["username"]; $str=mb_strlen($username); if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)." | a-z | 0-9 | A-Z| \_]+$/",$username) || $str<4 || $str>16) { echo "1"; } }
if(isset($_GET["username"])){ $username=$_GET["username"]; $str=mb_strlen($username); if(!preg_match("/(([\xB0-\xF7][\xA1-\xFE])|([\x81-\xA0][\x40-\xFE])|([\xAA-\xFE][\x40-\xA0])|(\w))+/",$username) || $str<4 || $str>16) { echo "1"; } }
$s ='我是ss中_文';$p='/^[\x{4e00}-\x{9fa5}\w]+$/u'; $bool=preg_match($p,$s);var_dump($bool); // 1
[解决办法]
你这是 gbk 编码的
if(preg_match('/^(?:\w|[\x80-\x9f\xb0-\xff].)+$/', $s))
对于 utf-8 编码要简单些
if(preg_match('^\w+$/u'))