| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- //执行验证规则
- /*
- 用法:
- Check::rule(
- array(验证函数1,'错误返回值1'),
- array(验证函数2,'错误返回值2'),
- );
- 若有一个验证函数返回false,则返回对应的错误返回值,若全部通过验证,则返回true。
- 验证函数,可以是自定义的函数或类方法,返回true表示通过,返回false,表示没有通过
- */
- namespace App\Repositories\Utils;
- class Check
- {
- public static function rule($array = array())
- {
- if (!isset($array[0][0])) {
- $array = func_get_args();
- }
- if (is_array($array)) {
- foreach ($array as $vo) {
- if (is_array($vo)&&isset($vo[0])&&isset($vo[1])) {
- if (!$vo[0]) {
- return $vo[1];
- }
- }
- }
- }
- return true;
- }
- /**
- * 检查字符串长度
- *
- * @access public
- * @param string $str 输入字符串
- * @param int $min 最小长度
- * @param int $max 最大长度
- * @return bool
- */
- public static function len($str, $min = 0, $max = 255)
- {
- $str = trim($str);
- if (empty($str)) {
- return true;
- }
- $len = dstrlen($str);
- if (($len>=$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;
- }
- }
- }
|