PHP實現(xiàn)的常規(guī)正則驗證helper公共類完整實例
本文實例講述了PHP實現(xiàn)的常規(guī)正則驗證helper公共類。分享給大家供大家參考,具體如下:
主要代碼功能: 彌補平時項目對于驗證功能這塊的不嚴謹。具體細分的常規(guī)驗證, 手機號/電話/小靈通驗證, 字符串長度區(qū)間合法驗證, 郵箱驗證, 使用正則驗證數(shù)據(jù).
/**
*
*
* 常規(guī)驗證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
* 聯(lián)通: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);
/**
* 中國聯(lián)通: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);
/**
* 大陸地區(qū)固話及小靈通
* 區(qū)號: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;
}
}
//字符串長度區(qū)間合法驗證
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;
}
/**
* 使用正則驗證數(shù)據(jù)
* @access public
* @param string $rule 驗證規(guī)則
* @param string $value 要驗證的數(shù)據(jù)
* @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})?$/',
//數(shù)字驗證
'number' => '/^[-\+]?\d+(\.\d+)?$/',
//zip驗證
'zip' => '/^\d{6}$/',
//整數(shù)驗證
'integer' => '/^[-\+]?\d+$/',
//浮點數(shù)驗證
'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}$/'
);
// 檢查是否有內(nèi)置的正則表達式
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;
}
}
is_null() 檢測變量是否為 NULL。
PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php正則表達式用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php運行出現(xiàn)Call to undefined function curl_init()的解決方法
curl_init -- 初始化一個CURL會話,如果提示Call to undefined function curl_init那么需要如下操作即可。2010-11-11
PHP微信企業(yè)號開發(fā)之回調(diào)模式開啟與用法示例
這篇文章主要介紹了PHP微信企業(yè)號開發(fā)之回調(diào)模式開啟與用法,結(jié)合具體實例形式分析了php微信企業(yè)號回調(diào)模式開啟與使用方法相關(guān)操作技巧,代碼中備有詳盡的注釋說明便于讀者理解,需要的朋友可以參考下2017-11-11
PHP中break及continue兩個流程控制指令區(qū)別分析
php中常用的for與foreach循環(huán)中,經(jīng)常遇到條件判斷或中止循環(huán)的情況。而處理方式主要用到break及continue兩個流程控制指令,現(xiàn)在說明主要區(qū)別2011-04-04

