编辑代码

<?php 
//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 


$string = "xample123d";
#$pattern = "/^[A-Za-z0-9@#%+-_\.]+$/";
#$pattern = "/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/";
#$pattern = '/^[a-zA-Z0-9_[@#¥&*!]\x7f-\xff][a-zA-Z0-9_[@#¥&*!]\x7f-\xff]+$/';
$pattern = '/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_#@!$&\x7f-\xff]+$/';
// if (preg_match($pattern,$string)) {
//     echo "字符串匹配成功";
// } else {
//     echo "字符串匹配失败";
// }



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

/**
 * 检查用户名是否符合规定
 *
 * @param STRING $username 要检查的用户名
 * @return 	TRUE or FALSE
 */
function is_username($username, $pattern) {
	$strlen = strlen($username);
	if(is_badword($username) || !preg_match($pattern, $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($string, $pattern));