<?php
class Verify{
#驗證用戶名,$value傳遞值;$minLen最小長度;$maxLen最長長度;只允許下劃線+漢字+英文+數字(不支持其它特殊字符)
#@param string $value
#@param int $length
#@return boolean
static function isUsername($value,$minLen=2,$maxLen=30){
if(!$value) return false;
return preg_match('/^[_wdx{4e00}-x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu',$value);
}
/**
* 是否為空
* @param type $value
* @return boolean
*/
static function isEmpty($value){
$value = trim($value);
if(empty($value)){
return true;
}else{
return false;
}
}
/**
* 判斷是否大于0
* @param unknown $value
* @return boolean
*/
static function isGtZero($value){
$value = (double)$value;
if($value>0){
return true;
}else{
return false;
}
}
#驗證是否為指定語言,$value傳遞值;$minLen最小長度;$maxLen最長長度;$charset默認字符類別(en只能英文;cn只能漢字;alb數字;ALL不限制)
#@param string $value
#@param int $length
#@return boolean
static function islanguage($value,$charset='all',$minLen=1,$maxLen=50){
if(!$value) return false;
switch($charset){
case 'en':$match = '/^[a-zA-Z]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'cn':$match = '/^[x{4e00}-x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'alb':$match = '/^[0-9]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'enalb':$match = '/^[a-zA-Z0-9]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'all':$match = '/^[a-zA-Z0-9x{4e00}-x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';break;
//all限制為:只能是英文或者漢字或者數字的組合
}
return preg_match($match,$value);
}
#驗證密碼,$value傳遞值;$minLen最小長度;$maxLen最長長度;
#@param string $value
#@param int $length
#@return boolean
static function isPassword($value,$minLen=6,$maxLen=16){//支持空格
$match='/^[\~!@#$%^&*() -_=+|{}[],.?/:;'"dw]{'.$minLen.','.$maxLen.'}$/i';
$value=trim($value);
if(!$value) return false;
return preg_match($match,$value);
}
#驗證eamil,$value傳遞值;$minLen最小長度;$maxLen最長長度;$match正則方式
#@param string $value
#@param int $length
#@return boolean
static function isEmail($value,$minLen=6,$maxLen=60,$match='/^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$/i'){
if(!$value) return false;
return (strlen($value)>=$minLen && strlen($value)<=$maxLen && preg_match($match,$value))?true:false;
}
#格式化money,$value傳遞值;小數點后最多2位
#@param string $value
#@return boolean
static function formatMoney($value){
return sprintf("%1$.2f",$value);
}
#驗證電話號碼,$value傳遞值;$match正則方式
#@param string $value
#@return boolean
static function isTelephone($value,$match='/^(0[1-9]{2,3})(-| )?d{7,8}$/'){
//支持國際版:$match='/^[+]?([0-9]){1,3}?[ |-]?(0[1-9]{2,3})(-| )?d{7,8}$/'
if(!$value) return false;
return preg_match($match,$value);
}
#驗證手機,$value傳遞值;$match正則方式
#@param string $value
#@param string $match
#@return boolean
static function isMobile($value,$match='/^(0)?1([3|4|5|8])+([0-9]){9,10}$/'){
//支持國際版:([0-9]{1,5}|0)?1([3|4|5|8])+([0-9]){9,10}
if(!$value) return false;
return preg_match($match,$value);
}
#驗證IP,$value傳遞值;$match正則方式
#@param string $value
#@param string $match
#@return boolean
static function isIP($value,$match='/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/'){
if(!$value) return false;
return preg_match($match,$value);
}
#驗證身份證號碼,$value傳遞值;$match正則方式
#@param string $value
#@param string $match
#@return boolean
static function isIDcard($value,$match='/^d{6}((1[89])|(2d))d{2}((0d)|(1[0-2]))((3[01])|([0-2]d))d{3}(d|X)$/i'){
if(!$value) return false;
else if(strlen($value)>18) return false;
return preg_match($match,$value);
}
#驗證URL,$value傳遞值;$match正則方式
#@param string $value
#@param string $match
#@return boolean
static function isURL($value,$match='/^(http://)?(https://)?([wd-]+.)+[w-]+(/[dw-./?%&=]*)?$/'){
$value=strtolower(trim($value));
if(!$value) return false;
return preg_match($match,$value);
}
}
//$verify=new verify();
?>
下載地址:http://pan.baidu.com/s/1o8M8qYQ
轉載地址:http://www.zuopeng.gd.cn/article/details/id/103.html