本文實例講述了PHP驗證類的封裝與使用方法。分享給大家供大家參考,具體如下:
?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用驗證 */ class Valid { static protected $error; static protected $error_tips = [ 'tel' => '手機號格式有誤', '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]; // 返回第一個錯誤 } 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(); }
封裝很有意思,這個類唯一的亮點,就是可以復合驗證。并且支持正則。而且里面的驗證方法還可以單獨使用。
vendor('Func.Valid'); if (!Valid::check_tel('152')) { $this->json->setErr(10001,'手機號有誤'); $this->json->Send(); }
勇敢的封裝,利國利民。
繼續(xù)封裝,支持數(shù)組傳參。
?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用驗證 */ class Valid { static protected $error; static protected $error_tips = [ 'tel' => '手機號格式有誤', '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]; // 返回第一個錯誤 } /** * @param $field string 驗證字段 * @param $rules string 驗證規(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]; // 返回第一個錯誤 } 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)化!支持錯誤提示中,添加參數(shù)。
?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用驗證 */ 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]; // 返回第一個錯誤 } /** * @param $field string 驗證字段 * @param $rules string 驗證規(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]; // 返回第一個錯誤 } /** * 靈活獲取參數(shù) * @param $rule * @param $param */ public static function get_error_tips($rule,$param) { $error_tips = [ 'tel' => '手機號格式有誤', '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款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php正則表達式用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。