最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php實(shí)現(xiàn)的表單驗(yàn)證類完整示例

 更新時(shí)間:2019年08月13日 09:19:31   作者:fangdong88  
這篇文章主要介紹了php實(shí)現(xiàn)的表單驗(yàn)證類,結(jié)合完整實(shí)例形式分析了php封裝的表單相關(guān)的正則驗(yàn)證、字符串轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)的表單驗(yàn)證類。分享給大家供大家參考,具體如下:

<?php
/**
 * 用法
 * use Validate\Validator;
 * 
 * $rules = [ 
 *    ['name|名字', 'require|email|in:7,8,9|max:10|min:6|between:6,8|length:2', '名字不能為空|名字必須必須為正確的郵件地址'],
 *    ['test|測(cè)試', 'require'],
 *  ];
 * $data = ['name' => '8gAg:'];
 * $validator = new Validator($rules);
 * $validator->addRule(['name|名字', 'regex', '/^[0-8|a-z]+$/', '正則驗(yàn)證失敗哦']); //可以為二維數(shù)組,有|的正則驗(yàn)證只能通過(guò)addRule。
 * $validator->validate($data);
 * $validator->getAllErrors(); //獲取所有驗(yàn)證錯(cuò)誤 array
 * $validator->getError(); //獲取第一條驗(yàn)證錯(cuò)誤 string
 * Validator::in('7,8,9', 8); //靜態(tài)調(diào)用
 * Validator::isEmail('375373223@qq.com');
 */
namespace Validate;
class Validator {
  //錯(cuò)誤信息
  private $error = [];
  //傳入的驗(yàn)證規(guī)則
  private $validate = [];
  //需要驗(yàn)證的參數(shù)
  private $data = [];
  //添加的規(guī)則
  private $add_rules = [];
  //默認(rèn)錯(cuò)誤提示
  private $error_msg = [
    'require' => ':attribute不能為空',
    'number' => ':attribute必須為數(shù)字',
    'array'  => ':attribute必須為數(shù)組',
    'float'  => ':attribute必須為浮點(diǎn)數(shù)',
    'boolean' => ':attribute必須為布爾值',
    'email'  => ':attribute必須為正確的郵件地址',
    'url'   => ':attribute必須為正確的url格式',
    'ip'   => ':attribute必須為正確的ip地址',
    'timestamp' => ':attribute必須為正確的時(shí)間戳格式',
    'date'  => ':attribute必須為正確的日期格式',
    'regex'  => ':attribute格式不正確',
    'in'   => ':attribute必須在:range內(nèi)',
    'notIn'  => ':attribute必須不在:range內(nèi)',
    'between' => ':attribute必須在:1-:2范圍內(nèi)',
    'notBetween' => ':attribute必須不在:1-:2范圍內(nèi)',
    'max'   => ':attribute最大值為:1',
    'min'   => ':attribute最小值為:1',
    'length' => ':attribute長(zhǎng)度必須為:1',
    'confirm' => ':attribute和:1不一致',
    'gt'   => ':attribute必須大于:1',
    'lt'   => ':attribute必須小于:1',
    'egt'   => ':attribute必須大于等于:1',
    'elt'   => ':attribute必須小于等于:1',
    'eq'   => ':attribute必須等于:1',
  ];
  public function __construct($validate = null) {
    $this->validate = $validate;
 }
  /**
   * [validate 驗(yàn)證]
   * @param [type] $data [需要驗(yàn)證的參數(shù)]
   * @return [type]    [boolean]
   */
 public function validate($data) {
 $this->data = $data;
    foreach ($this->validate as $key => $item) {
     $item_len = count($item);
     $name = $item[0];
     $rules = $item[1];
     $rules = explode('|', $rules);
     $message = $item_len > 2 ? explode('|', $item[2]) : null;
      $this->check($name, $rules, $message); 
    }
    if(!empty($this->add_rules)) {
     $this->checkAddRules();
    }
    return empty($this->error) ? TRUE : FALSE;
 }
  /**
   * [check 單個(gè)字段驗(yàn)證]
   * @param [type] $name  [description]
   * @param [type] $rules  [description]
   * @param [type] $message [description]
   * @return [type]     [null]
   */
 private function check($name, $rules, $message) {
 //$title = $name;
 $rule_name = $title = $name;
 if(strpos($name, '|')) {
  $rule_name = explode('|', $name)[0];
  $title = explode('|', $name)[1];
 }
    foreach ($rules as $i => $rule) {
   $validate_data = isset($this->data[$rule_name]) ? $this->data[$rule_name] : null;
     
     $result = $this->checkResult($rule, $validate_data);
     if(!$result) {
     $error_info = isset($message[$i]) ? $message[$i] : $this->getMessage($title, $rule);
        if($error_info) {
         array_push($this->error, $error_info);
        }
     }
    }
 }
  /**
   * [getMessage 獲取驗(yàn)證失敗的信息]
   * @param [type] $name [字段名]
   * @param [type] $rule [驗(yàn)證規(guī)則]
   * @return [type]    [string OR fail false]
   */
 private function getMessage($name, $rule) {
 $value1 = '';
 $value2 = '';
 $range = '';
 $error_key = $rule;
    if(strpos($rule, ':')) {
     $exp_arr = explode(':', $rule);
     $error_key = $exp_arr[0];
     $range = $exp_arr[1];
     $message_value = explode(',', $exp_arr[1]);
     $value1 = isset($message_value[0]) ? $message_value[0] : '';
     $value2 = isset($message_value[1]) ? $message_value[1] : '';
    }
    if(isset($this->error_msg[$error_key])) {
     return str_replace([':attribute', ':range', ':1', ':2'], [$name, $range, $value1, $value2], $this->error_msg[$error_key]);
    }
 return false;
 }
  /**
   * [checkResult 字段驗(yàn)證]
   * @param [type] $rule     [驗(yàn)證規(guī)則]
   * @param [type] $validate_data [需要驗(yàn)證的數(shù)據(jù)]
   * @return [type]        [boolean]
   */
 private function checkResult($rule, $validate_data) {
    switch ($rule) {
     case 'require':
       return $validate_data != '';
     break;
     case 'number':
       return filter_var($validate_data, FILTER_SANITIZE_NUMBER_INT);
     break;
     case 'array':
       return is_array($validate_data);
     break;
     case 'float':
       return filter_var($validate_data, FILTER_VALIDATE_FLOAT);
     break;
     case 'boolean':
       return filter_var($validate_data, FILTER_VALIDATE_BOOLEAN);
     break;
     case 'email':
       return filter_var($validate_data, FILTER_VALIDATE_EMAIL);
     break;
     case 'url':
       return filter_var($validate_data, FILTER_SANITIZE_URL);
     case 'ip':
       return filter_var($validate_data, FILTER_VALIDATE_IP);
     break;
     case 'timestamp':
       return strtotime(date('Y-m-d H:i:s',$validate_data)) == $validate_data;
     break;
     case 'date': //2017-11-17 12:12:12
       return strtotime($validate_data);
     break;
     default:
         if(strpos($rule, ':')) {
         $rule_arr = explode(':', $rule);
         $func_name = substr($rule, strpos($rule, ':')+1);
         return call_user_func_array([$this, $rule_arr[0]], [$func_name, $validate_data]); 
       }else{
        return call_user_func_array([$this, $rule], [$rule, $validate_data]); 
       }
     break;
    }
 }
  /**
   * [regex 正則驗(yàn)證]
   * @param [type] $rule [description]
   * @param [type] $data [description]
   * @return [type]    [description]
   */
 public static function regex($rule, $data) {
    return filter_var($data, FILTER_VALIDATE_REGEXP, ["options" => ["regexp" => $rule]]);
 }
  /**
   * [addRule 添加規(guī)則格式 []]
   * @param [type] $rule [description]
   */
 public function addRule($rule) {
 if(is_array(current($rule))) {
  $this->add_rules = array_merge($this->add_rules, $rule);
 }else{
  array_push($this->add_rules, $rule);
 }
 }
  /**
   * [checkAddRules 添加新的規(guī)則的驗(yàn)證]
   * @return [type] [description]
   */
 public function checkAddRules() {
 foreach ($this->add_rules as $key => $item) {
  $name = $item[0];
     $message = isset($item[3]) ? $item[3] : null;
     $rule_name = $title = $name;
  if(strpos($name, '|')) {
  $rule_name = explode('|', $name)[0];
  $title = explode('|', $name)[1];
  }
  $validate_data = isset($this->data[$rule_name]) ? $this->data[$rule_name] : null;
     
      $result = $this->checkResult($item[1].':'.$item[2], $validate_data);
     if(!$result) {
     $error_info = isset($message) ? $message : $this->getMessage($title, $item[1]);
       if($error_info) {
         array_push($this->error, $error_info);
       }
     } 
 }
 }
 /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function in($rule, $data) {
 if(!is_array($rule)) {
  $rule = explode(',', $rule);
 }
    return in_array($data, $rule);
 }
  /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function notIn($rule, $data) {
    return !$this->in($data, $rule);
 }
  /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function between($rule, $data) {
 $rule = explode(',', $rule);
    return $data >= $rule[0] && $data <= $rule[1];
 }
  /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function notBetween($rule, $data) {
 return !$this->between($rule, $data);
 }
  /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function max($rule, $data) {
 return $data <= $rule;
 }
  /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function min($rule, $data) {
 return $data >= $rule;
 }
  /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function length($rule, $data) {
 $length = is_array($data) ? count($data) : strlen($data);
 return $length == $rule;
 }
  /**
 * [in description]
 * @param [type] $rule [驗(yàn)證規(guī)則]
 * @param [type] $data [需要驗(yàn)證的數(shù)據(jù)]
 * @return [type]    [boolean]
 */
 public static function confirm($rule, $data) {
 return isset($this->data[$rule]) && $data == $this->data[$rule];
 }
 public static function gt($rule, $data) {
 return $data > $rule;
 }
  public static function lt($rule, $data) {
 return $data < $rule;
 }
 public static function egt($rule, $data) {
 return $data >= $rule;
 }
  public static function elt($rule, $data) {
 return $data <= $rule;
 }
 public static function eq($rule, $data) {
 return $data == $rule;
 }
  /**
 * [in 獲取驗(yàn)證失敗的第一條信息]
 * @return [type] [string]
 */
 public function getError() {
    return count($this->error) > 0 ? $this->error[0] : null;
 }
  /**
   * [getAllErrors 獲取所有驗(yàn)證失敗的信息]
   * @return [type] [array]
   */
 public function getAllErrors() {
    return $this->error;
 }
  /**
   * [__call 調(diào)用自定義函數(shù)或者]
   * @param [type] $func [驗(yàn)證規(guī)則,函數(shù)名]
   * @param [type] $data [驗(yàn)證數(shù)據(jù)]
   * @return [type]    [boollean]
   */
 function __call($func, $data) {
 $func_arr = get_defined_functions();
 if(in_array($func,$func_arr['user'])) {
  return call_user_func($func, $data);
 }else{
  array_push($this->error, '沒(méi)有' . $func . '這個(gè)方法');
 }
  }
  /**
   * [__callStatic 靜態(tài)方法調(diào)用自定義函數(shù)或者]
   * @param [type] $func [驗(yàn)證規(guī)則,函數(shù)名]
   * @param [type] $data [驗(yàn)證數(shù)據(jù)]
   * @return [type]    [boollean]
   */
  public static function __callStatic($func, $data) {
  if(substr($func, 0, 2) == 'is') {
  return call_user_func_array([new self, 'checkResult'], [strtolower(substr($func, 2)), $data[0]]);
 } 
  }
}

PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:

JavaScript正則表達(dá)式在線測(cè)試工具:
http://tools.jb51.net/regex/javascript

正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php程序設(shè)計(jì)安全教程》、《php安全過(guò)濾技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP基于GD庫(kù)的圖像處理方法小結(jié)

    PHP基于GD庫(kù)的圖像處理方法小結(jié)

    這篇文章主要介紹了PHP基于GD庫(kù)的圖像處理方法,結(jié)合實(shí)例形式總結(jié)分析了php操作GD庫(kù)實(shí)現(xiàn)圖形繪制功能的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2016-09-09
  • php實(shí)現(xiàn)二進(jìn)制和文本相互轉(zhuǎn)換的方法

    php實(shí)現(xiàn)二進(jìn)制和文本相互轉(zhuǎn)換的方法

    這篇文章主要介紹了php實(shí)現(xiàn)二進(jìn)制和文本相互轉(zhuǎn)換的方法,實(shí)例分析了文本與數(shù)制轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • PHP strtotime函數(shù)詳解

    PHP strtotime函數(shù)詳解

    strtotime函數(shù)是一個(gè)很好的函數(shù),靈活的運(yùn)用它,會(huì)給你的工作帶來(lái)不少方便.但PHP的手冊(cè)中卻對(duì)此函數(shù)的參數(shù)沒(méi)作太多介紹,對(duì)些函數(shù)的其他介紹也非常少。
    2009-12-12
  • 解析PHP高效率寫法(詳解原因)

    解析PHP高效率寫法(詳解原因)

    本篇文章是對(duì)PHP高效率寫法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 用PHP為SHOPEX增加日志功能代碼

    用PHP為SHOPEX增加日志功能代碼

    由于我的空間服務(wù)商并沒(méi)有提供IIS的日志功能。雖然頁(yè)面上放了CNZZ的站點(diǎn)統(tǒng)計(jì),可是詳細(xì)的頁(yè)面訪問(wèn)量和訪問(wèn)來(lái)源仍然表現(xiàn)的不夠徹底。
    2010-07-07
  • PHP守護(hù)進(jìn)程的兩種常見(jiàn)實(shí)現(xiàn)方式詳解

    PHP守護(hù)進(jìn)程的兩種常見(jiàn)實(shí)現(xiàn)方式詳解

    這篇文章主要介紹了PHP守護(hù)進(jìn)程的兩種常見(jiàn)實(shí)現(xiàn)方式,結(jié)合具體實(shí)例形式分析了php守護(hù)進(jìn)程的原理與相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2017-09-09
  • 實(shí)例解析php的數(shù)據(jù)類型

    實(shí)例解析php的數(shù)據(jù)類型

    在本篇文章中我們給大家分享了關(guān)于php的數(shù)據(jù)類型相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。
    2018-10-10
  • php求今天、昨天、明天時(shí)間戳的簡(jiǎn)單實(shí)現(xiàn)方法

    php求今天、昨天、明天時(shí)間戳的簡(jiǎn)單實(shí)現(xiàn)方法

    這篇文章主要介紹了php求今天、昨天、明天時(shí)間戳的方法,實(shí)例分析了strtotime函數(shù)的常見(jiàn)使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2016-07-07
  • PHP實(shí)現(xiàn)的單向散列加密操作示例

    PHP實(shí)現(xiàn)的單向散列加密操作示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)的單向散列加密操作,涉及PHP數(shù)據(jù)傳輸及加密解密等相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • php根據(jù)日期顯示所在星座的方法

    php根據(jù)日期顯示所在星座的方法

    這篇文章主要介紹了php根據(jù)日期顯示所在星座的方法,涉及php針對(duì)日期操作與流程控制的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07

最新評(píng)論

抚顺市| 舞阳县| 乌拉特中旗| 酒泉市| 固始县| 合水县| 高邑县| 太湖县| 台江县| 徐水县| 洞头县| 东方市| 秦安县| 伊金霍洛旗| 丹江口市| 玛多县| 唐山市| 金湖县| 慈利县| 高要市| 重庆市| 蓬安县| 常德市| 宣城市| 济源市| 南城县| 马鞍山市| 响水县| 东乌| 通河县| 孝义市| 吉木萨尔县| 长阳| 云南省| 舞钢市| 巴彦淖尔市| 高淳县| 墨玉县| 淮滨县| 长阳| 万源市|