好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > php實現的表單驗證類完整示例

php實現的表單驗證類完整示例

熱門標簽:江西手機自動外呼防封系統是什么 長春人工外呼系統服務商 怎么向銷售公司推銷外呼系統 廣州防封卡外呼系統多少錢一個月 高德地圖標注家 外呼系統撥打暫時無法接通 廣東地市地圖標注 哪里辦理400電話 仁和怎么申請400開頭的電話

本文實例講述了php實現的表單驗證類。分享給大家供大家參考,具體如下:

?php
/**
 * 用法
 * use Validate\Validator;
 * 
 * $rules = [ 
 *    ['name|名字', 'require|email|in:7,8,9|max:10|min:6|between:6,8|length:2', '名字不能為空|名字必須必須為正確的郵件地址'],
 *    ['test|測試', 'require'],
 *  ];
 * $data = ['name' => '8gAg:'];
 * $validator = new Validator($rules);
 * $validator->addRule(['name|名字', 'regex', '/^[0-8|a-z]+$/', '正則驗證失敗哦']); //可以為二維數組,有|的正則驗證只能通過addRule。
 * $validator->validate($data);
 * $validator->getAllErrors(); //獲取所有驗證錯誤 array
 * $validator->getError(); //獲取第一條驗證錯誤 string
 * Validator::in('7,8,9', 8); //靜態調用
 * Validator::isEmail('375373223@qq.com');
 */
namespace Validate;
class Validator {
  //錯誤信息
  private $error = [];
  //傳入的驗證規則
  private $validate = [];
  //需要驗證的參數
  private $data = [];
  //添加的規則
  private $add_rules = [];
  //默認錯誤提示
  private $error_msg = [
    'require' => ':attribute不能為空',
    'number' => ':attribute必須為數字',
    'array'  => ':attribute必須為數組',
    'float'  => ':attribute必須為浮點數',
    'boolean' => ':attribute必須為布爾值',
    'email'  => ':attribute必須為正確的郵件地址',
    'url'   => ':attribute必須為正確的url格式',
    'ip'   => ':attribute必須為正確的ip地址',
    'timestamp' => ':attribute必須為正確的時間戳格式',
    'date'  => ':attribute必須為正確的日期格式',
    'regex'  => ':attribute格式不正確',
    'in'   => ':attribute必須在:range內',
    'notIn'  => ':attribute必須不在:range內',
    'between' => ':attribute必須在:1-:2范圍內',
    'notBetween' => ':attribute必須不在:1-:2范圍內',
    'max'   => ':attribute最大值為:1',
    'min'   => ':attribute最小值為:1',
    'length' => ':attribute長度必須為: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 驗證]
   * @param [type] $data [需要驗證的參數]
   * @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 單個字段驗證]
   * @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 獲取驗證失敗的信息]
   * @param [type] $name [字段名]
   * @param [type] $rule [驗證規則]
   * @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 字段驗證]
   * @param [type] $rule     [驗證規則]
   * @param [type] $validate_data [需要驗證的數據]
   * @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 正則驗證]
   * @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 添加規則格式 []]
   * @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 添加新的規則的驗證]
   * @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 [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @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 [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @return [type]    [boolean]
 */
 public static function notIn($rule, $data) {
    return !$this->in($data, $rule);
 }
  /**
 * [in description]
 * @param [type] $rule [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @return [type]    [boolean]
 */
 public static function between($rule, $data) {
 $rule = explode(',', $rule);
    return $data >= $rule[0]  $data = $rule[1];
 }
  /**
 * [in description]
 * @param [type] $rule [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @return [type]    [boolean]
 */
 public static function notBetween($rule, $data) {
 return !$this->between($rule, $data);
 }
  /**
 * [in description]
 * @param [type] $rule [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @return [type]    [boolean]
 */
 public static function max($rule, $data) {
 return $data = $rule;
 }
  /**
 * [in description]
 * @param [type] $rule [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @return [type]    [boolean]
 */
 public static function min($rule, $data) {
 return $data >= $rule;
 }
  /**
 * [in description]
 * @param [type] $rule [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @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 [驗證規則]
 * @param [type] $data [需要驗證的數據]
 * @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 獲取驗證失敗的第一條信息]
 * @return [type] [string]
 */
 public function getError() {
    return count($this->error) > 0 ? $this->error[0] : null;
 }
  /**
   * [getAllErrors 獲取所有驗證失敗的信息]
   * @return [type] [array]
   */
 public function getAllErrors() {
    return $this->error;
 }
  /**
   * [__call 調用自定義函數或者]
   * @param [type] $func [驗證規則,函數名]
   * @param [type] $data [驗證數據]
   * @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, '沒有' . $func . '這個方法');
 }
  }
  /**
   * [__callStatic 靜態方法調用自定義函數或者]
   * @param [type] $func [驗證規則,函數名]
   * @param [type] $data [驗證數據]
   * @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款非常方便的正則表達式工具供大家參考使用:

JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript

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

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php程序設計安全教程》、《php安全過濾技巧總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • php封裝的表單驗證類完整實例
  • php常用表單驗證類用法實例
  • php 表單驗證實現代碼
  • php用戶注冊頁面利用js進行表單驗證具體實例
  • 基于PHP+Ajax實現表單驗證的詳解
  • php下常用表單驗證的正則表達式
  • PHP表單驗證內容是否為空的實現代碼
  • PHP Yii框架之表單驗證規則大全
  • PHP開發中常用的三個表單驗證函數使用小結
  • ThinkPHP框架表單驗證操作方法
  • PHP實現的用戶注冊表單驗證功能簡單示例

標簽:湘西 梅河口 廈門 黔東 文山 海北 惠州 濮陽

巨人網絡通訊聲明:本文標題《php實現的表單驗證類完整示例》,本文關鍵詞  php,實現,的,表單,驗證,類,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《php實現的表單驗證類完整示例》相關的同類信息!
  • 本頁收集關于php實現的表單驗證類完整示例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 他也噜他也噜在线视频播放| aa级毛片| 免费视频播放60分钟| 99riAV视频一区二区三区| 印度大肥妞bbwbbw| bl道具震动前列腺play| 成年人在线观看网站| 午夜精品99一区二区三区| 国产婷婷91秘?入口| chinese高中生勃起gay| 五月天色综合| 最近国产在线观看免费完整版| 火影忍者?18禁同人游戏| 亚洲丁香婷婷综合久久六月| 粉色视频完整观看免费| 国产乱子伦小说| 欧洲精品一二三区欧洲无码| 亲嘴扒胸摸屁股免费视频日本网站| 男生和女生差差软件| 日本爆乳成人风情影片| 别揉我奶?啊?嗯高潮张秘书| 一色屋成人免费精品网| 在线观看网站| 校长好大用力深一点| 大尺度床戏揉捏胸视频| 国产精品无码AV在线有声小说| 男男调教喷水沦为肉奴| 无敌手机网在线观看| 免费无遮挡??视频网站| 99精产国品一二三产区| 护士囗交10p| 国产色图片| javahdvideos日本hd| 91精品99精品国产高清色约约| 欧美成人综合在线| 亚洲精品老司机福利在线播放| 极品美女久久久久久久久久久| 《波多野结衣系列痴女CJOD-214》| GIFSGIFgifsexgifs| 欧美日韩一区二区不卡三区| 国产午夜毛片v一区二区三区|