=$min)&&($len<=$max)) { return true; } else { return false; } } /** * 检查字符串是否为空 * * @access public * @param string $str 输入字符串 * @return bool */ public static function must($str) { $str = trim($str); return !empty($str); } /** * 检查两次输入的值是否相同 * * @access public * @param string $str1 输入字符串1 * @param string $str2 输入字符串2 * @return bool */ public static function same($str1, $str2) { return $str1 == $str2; } /** * 检查用户名 * * @access public * @param string $str 输入字符串 * @param integer $len_min 最小长度 * @param integer $len_max 最大长度 * @param string $type 判断类型 * @return bool */ public static function userName($str, $len_min = 0, $len_max = 255, $type = 'ALL') { if (empty($str)) { return true; } if (self::len($str, $len_min, $len_max) == false) { return false; } switch ($type) { //纯英文 case "EN": $pattern = "/^[a-zA-Z]+$/"; break; //英文数字 case "ENNUM": $pattern = "/^[a-zA-Z0-9]+$/"; break; //by ninja911 包含中文,至少一个汉字、数字、字母、下划线(不能以_开头和结尾) case "CHINESE": $pattern = "/^(?!_)(?!.*?_$)[a-zA-Z0-9_\x{4e00}-\x{9fc3}]+$/u"; break; //允许的符号(|-_字母数字) case "CHINESE2": $pattern = "/^[a-zA-Z\x{4e00}-\x{9fc3}][a-zA-Z0-9_\x{4e00}-\x{9fc3}]+$/u"; break; //允许的符号(|-_字母数字) case "ALL": $pattern = "/^[\-\_a-zA-Z0-9]+$/"; break; //用户自定义正则 default: $pattern = $type; break; } if (preg_match($pattern, $str)) { return true; } else { return false; } } /** * 验证邮箱 * * @access public * @param string $str 输入字符串 * @return bool */ public static function email($str) { if (empty($str)) { return true; } $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i"; if (strpos($str, '@') !== false && strpos($str, '.') !== false) { if (preg_match($chars, $str)) { return true; } else { return false; } } else { return false; } } /** * 验证手机号码 * * @access public * @param string $str 输入字符串 * @return bool */ public static function mobile($str) { if (empty($str)) { return true; } return preg_match('#^13[\d]{9}$|14^[0-9]\d{8}|^15[0-9]\d{8}$|^18[0-9]\d{8}$|^17[0-9]\d{8}$#', $str); } /** * 验证固定电话 * * @access public * @param string $str 输入字符串 * @return bool */ public static function tel($str) { if (empty($str)) { return true; } return preg_match('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', trim($str)); } /** * 验证qq号码 * * @access public * @param string $str 输入字符串 * @return bool */ public static function qq($str) { if (empty($str)) { return true; } return preg_match('/^[1-9]\d{4,12}$/', trim($str)); } /** * 验证邮政编码 * * @access public * @param string $str 输入字符串 * @return bool */ public static function zipCode($str) { if (empty($str)) { return true; } return preg_match('/^[1-9]\d{5}$/', trim($str)); } /** * 验证ip * * @access public * @param string $str 输入字符串 * @return bool */ public static function ip($str) { if (empty($str)) { return true; } if (!preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $str)) { return false; } $ip_array = explode('.', $str); //真实的ip地址每个数字不能大于255(0-255) return ($ip_array[0]<=255 && $ip_array[1]<=255 && $ip_array[2]<=255 && $ip_array[3]<=255) ? true : false; } /** * 验证身份证(中国) * * @access public * @param string $str 输入字符串 * @return bool */ public static function idCard($str) { $str = trim($str); if (empty($str)) { return true; } if (preg_match("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i", $str)) { return true; } else { return false; } } /** * 验证网址 * * @access public * @param string $str 输入字符串 * @return bool */ public static function url($str) { if (empty($str)) { return true; } return preg_match( "/^(http|https|ftp|ftps):\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/", $str ) ? true : false; } /** * 判断目录名是否合法 * * @access public * @param string $str 输入字符串 * @return bool */ public static function dirName($str) { $str = !empty($str) ? trim($str) : ''; if (empty($str)) { return false; } return preg_match('/^(?!\\/)[\-\_\\/a-zA-Z0-9]+$/', $str); } /** * 判断是否为指定长度内字符串 * * @access public * @param string $str 输入字符串 * @param integer $min 最小长度 * @param integer $max 最大长度 * @return bool */ public static function lengthBetween($str, $min = 0, $max = 0) { $str_length = $str ? dstrlen($str) : 0; if ($min > 0 && $max > 0) { if (!($str_length >= $min && $str_length <= $max)) { return false; } else { return true; } } elseif ($min > 0 && $max == 0) { if ($str_length < $min) { return false; } else { return true; } } elseif ($max > 0 && $min == 0) { if ($str_length > $max) { return false; } else { return true; } } else { return true; } } /** * 判断文件扩展名合法 * * @access public * @param string $file_name 文件名 * @param array $allow_ext 允许扩展名数组 * @return bool */ public static function fileAllowExt($file_name, $allow_ext = array()) { $file_ext = file_ext($file_name); if ($file_ext) { if ($allow_ext) { if (!in_array($file_ext, $allow_ext)) { return false; } else { return true; } } else { return true; } } else { return false; } } }