Home > Article > Backend Development > 只允许汉字,字母,数字和停横线组合的正则
只允许汉字,字母,数字和下横线组合的正则
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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) || $str16) { echo "1"; } }
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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) || $str16) { echo "1"; } }
$s ='我是ss中_文'; $p='/^[\x{4e00}-\x{9fa5}\w]+$/u'; $bool=preg_match($p,$s); var_dump($bool); // 1 <br><font color="#e78608">------解决方案--------------------</font><br> 你这是 gbk 编码的<br>if(preg_match('/^(?:\w|[\x80-\x9f\xb0-\xff].)+$/', $s))<br><br>对于 utf-8 编码要简单些<br>if(preg_match('^\w+$/u'))<br> <div class="clear"> </div>