检查输入的用户名是否符合规定

代码清单:

<?php
 /**
 * 检测输入中是否含有错误字符
 * @author ruxing.li
 * @param char $string 要检查的字符串名称
 * @return boolean
 */
function is_badword($string) {
    $badwords = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n","#");
    foreach($badwords as $value){
        if(strpos($string, $value) !== false) {
            return true;
        }
    }
    return false;
}

/**
 * 检查用户名是否符合规定
 * @author ruxing.li
 * @param  string $username 要检查的用户名
 * @return boolean
 */
function is_username($username) {
    $strlen = strlen($username);
    if(is_badword($username) || !preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $username)){
        return false;
    } elseif ( 20 < $strlen || $strlen < 2 ) {
        return false;
    }
    return true;
}

var_dump(is_username('ruxing.li')); //boolean false
var_dump(is_username('ruxingli'));  //boolean true

声明:本文来自CSDN,若转载请注明出处!

你可能感兴趣的:(PHP)