PHP驗(yàn)證類的封裝與使用方法詳解
本文實(shí)例講述了PHP驗(yàn)證類的封裝與使用方法。分享給大家供大家參考,具體如下:
<?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用驗(yàn)證 */ class Valid { static protected $error; static protected $error_tips = [ 'tel' => '手機(jī)號(hào)格式有誤', 'email' => '郵箱格式有誤', 'max_len' => '參數(shù)長度不能超過最大長度', 'min_len' => '參數(shù)長度不能小于最小長度', 'required' => '缺少參數(shù)' ]; // required|max_len,100|min_len,6 public function validate($field, $rules) { $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($field,$param)) { self::$error[] = self::$error_tips[$rule] ? self::$error_tips[$rule] : '參數(shù)格式有誤'; } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一個(gè)錯(cuò)誤 } public static function check_required($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } public static function check_tel($field) { if(preg_match("/^1[345678]{1}\d{9}$/",$field)){ return true; }else{ return false; } } public static function check_email($field) { if(preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$field)){ return true; }else{ return false; } } public static function check_max_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) <= (int) $param) { return true; } else { return false; } } else { if (strlen($field) <= (int) $param) { return true; } else { return false; } } } public static function check_min_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) >= (int) $param) { return true; } else { return false; } } else { if (strlen($field) >= (int) $param) { return true; } else { return false; } } } public static function check_regex($field, $param = null) { $regex = $param; if (preg_match($regex, $field)) { return true; } else { return false; } } }
基本滿足需求。
vendor('Func.Valid'); if ($res = Valid::validate('152','required|regex,/^1[345678]{1}\d{9}$/')) { $this->json->setErr(10001,$res); $this->json->Send(); }
封裝很有意思,這個(gè)類唯一的亮點(diǎn),就是可以復(fù)合驗(yàn)證。并且支持正則。而且里面的驗(yàn)證方法還可以單獨(dú)使用。
vendor('Func.Valid'); if (!Valid::check_tel('152')) { $this->json->setErr(10001,'手機(jī)號(hào)有誤'); $this->json->Send(); }
勇敢的封裝,利國利民。
繼續(xù)封裝,支持?jǐn)?shù)組傳參。
<?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用驗(yàn)證 */ class Valid { static protected $error; static protected $error_tips = [ 'tel' => '手機(jī)號(hào)格式有誤', 'email' => '郵箱格式有誤', 'max_len' => '參數(shù)長度不能超過最大長度', 'min_len' => '參數(shù)長度不能小于最小長度', 'required' => '缺少參數(shù)' ]; /** * @param $validators array array('email' => 'required|valid_email') * @param $input array post數(shù)據(jù) * @return string */ public function is_valid($validators, $input) { foreach ($validators as $field => $rules) { if (!isset($input[$field]) || empty($input[$field])) { self::$error[] = "缺少參數(shù)"; } $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($input[$field],$param)) { self::$error[] = self::$error_tips[$rule] ? self::$error_tips[$rule] : '參數(shù)格式有誤'; } } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一個(gè)錯(cuò)誤 } /** * @param $field string 驗(yàn)證字段 * @param $rules string 驗(yàn)證規(guī)則 required|max_len,100|min_len,6 * @return string */ public function validate($field, $rules) { $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($field,$param)) { self::$error[] = self::$error_tips[$rule] ? self::$error_tips[$rule] : '參數(shù)格式有誤'; } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一個(gè)錯(cuò)誤 } public static function check_required($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } /** * 簡寫 * @param $field * @return bool */ public static function check_r($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } public static function check_tel($field) { if(preg_match("/^1[345678]{1}\d{9}$/",$field)){ return true; }else{ return false; } } public static function check_email($field) { if(preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$field)){ return true; }else{ return false; } } public static function check_max_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) <= (int) $param) { return true; } else { return false; } } else { if (strlen($field) <= (int) $param) { return true; } else { return false; } } } public static function check_min_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) >= (int) $param) { return true; } else { return false; } } else { if (strlen($field) >= (int) $param) { return true; } else { return false; } } } public static function check_regex($field, $param = null) { $regex = $param; if (preg_match($regex, $field)) { return true; } else { return false; } } }
使用如下
vendor('Func.Valid'); $validators = [ 'tel' => 'required|tel', 'name' => 'required', 'email' => 'r|email', 'password' => 'r|min_len,6|max_len,12' ]; if ($err = Valid::is_valid($validators,$_POST)) { $this->json->setErr(10001,$err); $this->json->Send(); }
繼續(xù)優(yōu)化!支持錯(cuò)誤提示中,添加參數(shù)。
<?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用驗(yàn)證 */ class Valid { static protected $error; /** * @param $validators array array('email' => 'required|valid_email') * @param $input array post數(shù)據(jù) * @return string */ public function is_valid($validators, $input) { foreach ($validators as $field => $rules) { if (!isset($input[$field]) || empty($input[$field])) { self::$error[] = "缺少參數(shù)"; } $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($input[$field],$param)) { self::$error[] = self::get_error_tips($rule,$param); } } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一個(gè)錯(cuò)誤 } /** * @param $field string 驗(yàn)證字段 * @param $rules string 驗(yàn)證規(guī)則 required|max_len,100|min_len,6 * @return string */ public function validate($field, $rules) { $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($field,$param)) { self::$error[] = self::get_error_tips($rule,$param); } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一個(gè)錯(cuò)誤 } /** * 靈活獲取參數(shù) * @param $rule * @param $param */ public static function get_error_tips($rule,$param) { $error_tips = [ 'tel' => '手機(jī)號(hào)格式有誤', 'email' => '郵箱格式有誤', 'max_len' => '參數(shù)長度不能超過最大長度'.$param, 'min_len' => '參數(shù)長度不能小于最小長度'.$param, 'required' => '缺少參數(shù)', 'r' => '缺少參數(shù)' ]; return $error_tips[$rule] ? $error_tips[$rule] : '參數(shù)格式有誤'; } public static function check_required($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } /** * 簡寫 * @param $field * @return bool */ public static function check_r($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } public static function check_tel($field) { if(preg_match("/^1[345678]{1}\d{9}$/",$field)){ return true; }else{ return false; } } public static function check_email($field) { if(preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$field)){ return true; }else{ return false; } } public static function check_max_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) <= (int) $param) { return true; } else { return false; } } else { if (strlen($field) <= (int) $param) { return true; } else { return false; } } } public static function check_min_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) >= (int) $param) { return true; } else { return false; } } else { if (strlen($field) >= (int) $param) { return true; } else { return false; } } } public static function check_regex($field, $param = null) { $regex = $param; if (preg_match($regex, $field)) { return true; } else { return false; } } }
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php正則表達(dá)式用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP中數(shù)組轉(zhuǎn)換為SimpleXML教程
在本篇文章中我們給大家總結(jié)了一篇關(guān)于PHP中數(shù)組轉(zhuǎn)換為SimpleXML教程內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。2019-01-01PHP與MySQL開發(fā)中頁面出現(xiàn)亂碼的一種解決方法
PHP與MySQL開發(fā)中頁面出現(xiàn)亂碼的一種解決方法...2007-07-07使用git遷移Laravel項(xiàng)目至新開發(fā)環(huán)境的步驟詳解
這篇文章主要介紹了遷移Laravel項(xiàng)目至新開發(fā)環(huán)境的步驟詳解,需要的朋友可以參考下2020-04-04PHP中sleep()函數(shù)的實(shí)用場景以及注意事項(xiàng)
sleep()函數(shù)是PHP中的一個(gè)休眠函數(shù),可以讓程序在指定的時(shí)間內(nèi)暫停執(zhí)行,以達(dá)到延遲執(zhí)行的效果,本文介紹使用sleep()函數(shù)的實(shí)用場景以及注意事項(xiàng)2023-09-09PHP7實(shí)現(xiàn)和CryptoJS的AES加密方式互通示例【AES-128-ECB加密】
這篇文章主要介紹了PHP7實(shí)現(xiàn)和CryptoJS的AES加密方式互通操作,結(jié)合實(shí)例形式分析了PHP AES-128-ECB加密算法相關(guān)使用技巧,需要的朋友可以參考下2019-06-06php支持?jǐn)帱c(diǎn)續(xù)傳、分塊下載的類
這篇文章主要介紹了php支持?jǐn)帱c(diǎn)續(xù)傳、分塊下載的類的相關(guān)資料,需要的朋友可以參考下2016-05-05如何在thinkphp中使用windows計(jì)劃任務(wù)定時(shí)執(zhí)行php文件
這篇文章主要介紹了如何在thinkphp中使用windows計(jì)劃任務(wù)定時(shí)執(zhí)行php文件,對(duì)定時(shí)執(zhí)行感興趣的同學(xué),可以參考下2021-04-04