- /**
- * Verification class
- *
- * @lastmodify 2014-5-16
- * @author jy625
- */
- class VerifyAction{
- /**
- * Whether it is a null value
- */
- public static function isEmpty($str){
- $str = trim($str);
- return !empty($str) ? true : false;
- }
- /**
- * Number verification
- * param:$flag: whether int is an integer and float is a floating point type
- */
- public static function isNum($str,$flag = 'float'){
- if(!self::isEmpty($str)) return false;
- if(strtolower($flag) == 'int'){
- return ((string)(int)$str === (string)$str) ? true : false;
- }else{
- return ((string)(float)$str === (string)$str) ? true : false;
- }
- }
- /**
- * Name matching, such as user name, directory name, etc.
- * @param:string $str The string to be matched
- * @param:$chinese Whether to support Chinese, supported by default. If it is matching file names, it is recommended to turn this off ( false)
- * @param:$charset encoding (default utf-8, supports gb2312)
- */
- public static function isName($str,$chinese = true,$charset = 'utf-8'){
- if(!self::isEmpty($str)) return false;
- if($chinese){
- $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_-]+$/" : "/^[x{4e00}-x{9fa5}A-Za-z0-9_]+$/u";
- }else{
- $match = '/^[A-za-z0-9_-]+$/';
- }
- return preg_match($match,$str) ? true : false;
- }
- /**
- * Email verification
- */
- public static function isEmail($str){
- if(!self::isEmpty($str)) return false;
- return preg_match("/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i",$str) ? true : false;
- }
- //手机号码验证
- public static function isMobile($str){
- $exp = "/^13[0-9]小贝[0-9]{8}$|15[012356789]小贝[0-9]{8}$|18[012356789]小贝[0-9]{8}$|14[57]小贝[0-9]$/";
- if(preg_match($exp,$str)){
- return true;
- }else{
- return false;
- }
- }
- /**
- * URL verification, pure URL format, does not support IP verification
- */
- public static function isUrl($str){
- if(!self::isEmpty($str)) return false;
- return preg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str) ? true : false;
- }
- /**
- * Verify Chinese
- * @param:string $str The string to be matched
- * @param:$charset encoding (default utf-8, support gb2312)
- */
- public static function isChinese($str,$charset = 'utf-8'){
- if(!self::isEmpty($str)) return false;
- $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/"
- : "/^[x{4e00}-x{9fa5}]+$/u";
- return preg_match($match,$str) ? true : false;
- }
- /**
- * UTF-8 validation
- */
- public static function isUtf8($str){
- if(!self::isEmpty($str)) return false;
- return (preg_match("/^([".chr(228)."-".chr(233)."]小贝[".chr(128)."-".chr(191)."]小贝[".chr(128)."-".chr(191)."]小贝)小贝/",$word)
- == true || preg_match("/([".chr(228)."-".chr(233)."]小贝[".chr(128)."-".chr(191)."]小贝[".chr(128)."-".chr(191)."]小贝)小贝$/",$word)
- == true || preg_match("/([".chr(228)."-".chr(233)."]小贝[".chr(128)."-".chr(191)."]小贝[".chr(128)."-".chr(191)."]小贝){2,}/",$word)
- == true) ? true : false;
- }
- /**
- * Verification length
- * @param: string $str
- * @param: int $type (method, default min <= $str <= max)
- * @param: int $min, minimum value; $max, Maximum value;
- * @param: string $charset character
- */
- public static function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){
- if(!self::isEmpty($str)) return false;
- $len = mb_strlen($str,$charset);
- switch($type){
- case 1: //只匹配最小值
- return ($len >= $min) ? true : false;
- break;
- case 2: //只匹配最大值
- return ($max >= $len) ? true : false;
- break;
- default: //min <= $str <= max
- return (($min <= $len) && ($len <= $max)) ? true : false;
- }
- }
- /**
- * Verification password
- * @param string $value
- * @param int $length
- * @return boolean
- */
- public static function isPWD($value,$minLen=6,$maxLen=16){
- $match='/^[\~!@#$%^&*()-_=+|{}
- ,.?/:;'"dw]{'.$minLen.','.$maxLen.'}$/';
- $v = trim($value);
- if(empty($v))
- return false;
- return preg_match($match,$v);
- }
- /**
- * Verify username
- * @param string $value
- * @param int $length
- * @return boolean
- */
- public static function isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
- if(empty($value))
- return false;
- switch($charset){
- case 'EN': $match = '/^[_wd]{'.$minLen.','.$maxLen.'}$/iu';
- break;
- case 'CN':$match = '/^[_x{4e00}-x{9fa5}d]{'.$minLen.','.$maxLen.'}$/iu';
- break;
- default:$match = '/^[_wdx{4e00}-x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
- }
- return preg_match($match,$value);
- }
- /**
- * Verification email
- * @param string $value
- */
- public static function checkZip($str){
- if(strlen($str)!=6){
- return false;
- }
- if(substr($str,0,1)==0){
- return false;
- }
- return true;
- }
- /**
- * Match date
- * @param string $value
- */
- public static function checkDate($str){
- $dateArr = explode("-", $str);
- if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
- if (($dateArr[0] >= 1000 && $timeArr[0] <= 10000) && ($dateArr[1] >= 0 && $dateArr[1] <= 12) && ($dateArr[2] >= 0 && $dateArr[2] <= 31))
- return true;
- else
- return false;
- }
- return false;
- }
- /**
- * Matching time
- * @param string $value
- */
- public static function checkTime($str){
- $timeArr = explode(":", $str);
- if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
- if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
- return true;
- else
- return false;
- }
- return false;
- }
- }
复制代码
|