比如我写好的POST提交例子,验证邮箱和值是否未空?include("check.php");$check=newVerifyAction();if(?){echo'提交成功!';}else{echo'填写不对重填写';} 验证类check.php/***验证类**@lastmodify2014-5-16*@authorjy625*/classVerifyAction{/***是否为空值*/publicstaticfunctionisEmpty($str){$str=trim($str);return!empty($str)?true:false;}/***数字验证*param:$flag:int是否是整数,float是否是浮点型*/publicstaticfunctionisNum($str,$flag='float'){if(!self::isEmpty($str))returnfalse;if(strtolower($flag)=='int'){return((string)(int)$str===(string)$str)?true:false;}else{return((string)(float)$str===(string)$str)?true:false;}}/***名称匹配,如用户名,目录名等*@param:string$str要匹配的字符串*@param:$chinese是否支持中文,默认支持,如果是匹配文件名,建议关闭此项(false)*@param:$charset编码(默认utf-8,支持gb2312)*/publicstaticfunctionisName($str,$chinese=true,$charset='utf-8'){if(!self::isEmpty($str))returnfalse;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_-]+$/';}returnpreg_match($match,$str)?true:false;}/***邮箱验证*/publicstaticfunctionisEmail($str){if(!self::isEmpty($str))returnfalse;returnpreg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$str)?true:false;}//手机号码验证publicstaticfunctionisMobile($str){$exp="/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/";if(preg_match($exp,$str)){returntrue;}else{returnfalse;}}/***URL验证,纯网址格式,不支持IP验证*/publicstaticfunctionisUrl($str){if(!self::isEmpty($str))returnfalse;returnpreg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str)?true:false;}/***验证中文*@param:string$str要匹配的字符串*@param:$charset编码(默认utf-8,支持gb2312)*/publicstaticfunctionisChinese($str,$charset='utf-8'){if(!self::isEmpty($str))returnfalse;$match=(strtolower($charset)=='gb2312')?"/^[".chr(0xa1)."-".chr(0xff)."]+$/":"/^[x{4e00}-x{9fa5}]+$/u";returnpreg_match($match,$str)?true:false;}/***UTF-8验证*/publicstaticfunctionisUtf8($str){if(!self::isEmpty($str))returnfalse;return(preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word)==true||preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word)==true||preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word)==true)?true:false;}/***验证长度*@param:string$str*@param:int$type(方式,默认min<=$str<=max)*@param:int$min,最小值;$max,最大值;*@param:string$charset字符*/publicstaticfunctionlength($str,$type=3,$min=0,$max=0,$charset='utf-8'){if(!self::isEmpty($str))returnfalse;$len=mb_strlen($str,$charset);switch($type){case1://只匹配最小值return($len>=$min)?true:false;break;case2://只匹配最大值return($max>=$len)?true:false;break;default://min<=$str<=maxreturn(($min<=$len)&&($len<=$max))?true:false;}}/***验证密码*@paramstring$value*@paramint$length*@returnboolean*/publicstaticfunctionisPWD($value,$minLen=6,$maxLen=16){$match='/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';$v=trim($value);if(empty($v))returnfalse;returnpreg_match($match,$v);}/***验证用户名*@paramstring$value*@paramint$length*@returnboolean*/publicstaticfunctionisNames($value,$minLen=2,$maxLen=16,$charset='ALL'){if(empty($value))returnfalse;switch($charset){case'EN':$match='/^[_\w\d]{'.$minLen.','.$maxLen.'}$/iu';break;case'CN':$match='/^[_\x{4e00}-\x{9fa5}\d]{'.$minLen.','.$maxLen.'}$/iu';break;default:$match='/^[_\w\d\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';}returnpreg_match($match,$value);}/***验证邮箱*@paramstring$value*/publicstaticfunctioncheckZip($str){if(strlen($str)!=6){returnfalse;}if(substr($str,0,1)==0){returnfalse;}returntrue;}/***匹配日期*@paramstring$value*/publicstaticfunctioncheckDate($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))returntrue;elsereturnfalse;}returnfalse;}/***匹配时间*@paramstring$value*/publicstaticfunctioncheckTime($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))returntrue;elsereturnfalse;}returnfalse;}}
慕盖茨4494581
相关分类