《PHP實例:PHP實現的常規正則驗證helper公共類完整實例》要點:
本文介紹了PHP實例:PHP實現的常規正則驗證helper公共類完整實例,希望對您有用。如果有疑問,可以聯系我們。
PHP應用本文實例講述了PHP實現的常規正則驗證helper公共類.分享給大家供大家參考,具體如下:
PHP應用主要代碼功能: 彌補平時項目對于驗證功能這塊的不嚴謹.具體細分的常規驗證, 手機號/電話/小靈通驗證, 字符串長度區間合法驗證, 郵箱驗證, 使用正則驗證數據.
PHP應用
/**
*
*
* 常規驗證helper公共類
*
*
*/
class CheckForm
{
//手機號/電話/小靈通 驗證
public function Mobile_check($mobile,$type = array())
{
/**
* 手機號碼
* 移動:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 聯通:130,131,132,152,155,156,185,186
* 電信:133,1349,153,180,189
*/
$res[1]= preg_match('/^1(3[0-9]|5[0-35-9]|8[0-9])\\d{8}$/', $mobile);
/**
* 中國移動:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
*/
$res[2]= preg_match('/^1(34[0-8]|(3[5-9]|5[017-9]|8[0-9])\\d)\\d{7}$/', $mobile);
/**
* 中國聯通:China Unicom
* 130,131,132,152,155,156,185,186
*/
$res[3]= preg_match('/^1(3[0-2]|5[256]|8[56])\\d{8}$/', $mobile);
/**
* 中國電信:China Telecom
* 133,1349,153,180,189
*/
$res[4]= preg_match('/^1((33|53|8[09])[0-9]|349)\\d{7}$/', $mobile);
/**
* 大陸地區固話及小靈通
* 區號:010,020,021,022,023,024,025,027,028,029
* 號碼:七位或八位
*/
$res[5]= preg_match('/^0(10|2[0-5789]|\\d{3})-\\d{7,8}$/', $mobile);
$type = empty($type) ? array(1,2,3,4,5) : $type;
$ok = false;
foreach ($type as $key=>$val)
{
if ($res[$val])
{
$ok = true;
}
continue;
}
if ( $mobile && $ok )
{
return true;
} else{
return false;
}
}
//字符串長度區間合法驗證
public function Strlength_check($str, $min=NULL, $max=NULL)
{
preg_match_all("/./u", $str, $matches);
$len = count($matches[0]);
if(is_null($min) && !empty($max) && $len < $max){
return false;
}
if(is_null($max) && !empty($min) && $len > $min){
return false;
}
if ($len < $min || $len > $max) {
return false;
}
return true;
}
//郵箱驗證
public static function isEmail($str)
{
if (!$str) {
return false;
}
return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $str) ? true : false;
}
/**
* 使用正則驗證數據
* @access public
* @param string $rule 驗證規則
* @param string $value 要驗證的數據
* @return boolean
*/
public function regex($rule,$value) {
$validate = array(
//字段必須,不能為空
'require' => '/\S+/',
//郵箱驗證
'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
//url驗證
'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
//貨幣驗證
'currency' => '/^\d+(\.\d{0,2})?$/',
//數字驗證
'number' => '/^[-\+]?\d+(\.\d+)?$/',
//zip驗證
'zip' => '/^\d{6}$/',
//整數驗證
'integer' => '/^[-\+]?\d+$/',
//浮點數驗證
'double' => '/^[-\+]?\d+(\.\d+)?$/',
//英文驗證
'english' => '/^[A-Za-z]+$/',
'gt0' => '/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/',
//合法帳號
'account' => '/^[a-zA-Z][a-zA-Z0-9_]{1,19}$/'
);
// 檢查是否有內置的正則表達式
if(isset($validate[strtolower($rule)]))
$rule = $validate[strtolower($rule)];
return preg_match($rule,$value)===1;
}
function CheckPwd($pwd,$min=NULL, $max=NULL)
{
if (strlen($pwd)>$max || strlen($pwd)<$min || preg_match("/^\d*$/",$pwd) || preg_match("/^[a-z]*$/i",$pwd))
{
return false;
}
return true;
}
}
PHP應用is_null()
檢測變量是否為 NULL.
PHP應用PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
PHP應用JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
PHP應用正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
PHP應用更多關于PHP相關內容感興趣的讀者可查看本站專題:《php正則表達式用法總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網絡編程技巧總結》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP應用希望本文所述對大家PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/908.html