Home  >  Article  >  Backend Development  >  【正则】如何写判断提交的用户名中不包含如下特殊字符?求方法

【正则】如何写判断提交的用户名中不包含如下特殊字符?求方法

WBOY
WBOYOriginal
2016-06-23 13:56:161695browse

对提交的注册用户名 $uname 的需求:
1 要求名称可以由中文 英文混合组成,但是中间不能包含有空格
2 不能包含如下 $arr2 中的特殊字符
3 但是可以包含中文的 左右括号 “(” “)”

<?phpfunction checkReg($uname){/**//写成简短的$arr2 = array('~', '!', '@', '#', '$', '%', '^', '&', '*', '_', '+', '|', '-', '=', '\\','{', '}', '[', ']', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/', '“', '”','’', '‘', '【', '】', '~', '!', '¥', '……', '??', '、', '《', '》', '。',PHP_EOL, chr(10), chr(13), "\t", chr(32),);**/    $arr2 = array(        '~',        '!',        '@',        '#',        '$',        '%',        '^',        '&',        '*',        '_',        '+',        '|',        '-',        '=',        '\\',        '{',        '}',        '[',        ']',        ':',        ';',        '"',        '\'',        '<',        '>',        ',',        '.',        '?',        '/',        '“',        '”',        '’',        '‘',        '【',        '】',        '~',        '!',        '¥',        '……',        '??',        '、',        '《',        '》',        '。',        PHP_EOL,        chr(10),        chr(13), //\r\n        "\t",        chr(32),    );    foreach ($arr2 as $k) {        //我之前用 preg_match 结果有乱码,全乱了。后来用strpos  (default7#zbphp.com)        if (strpos($uname, $k) !== false) {            $tips = "注册名中不能含有空格、点、逗号等特殊字符!";            return array(false, $tips);        }    }    return array(true, 'ok');}



回复讨论(解决方案)

if(strlen($s) == strlen(str_replace($arr2, '', $s))) {  //不包含特殊字符}else{ //包含特殊字符}

if($s == str_replace($arr2, '', $s)) {  //不包含特殊字符}else{ //包含特殊字符}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn