濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > PHP驗(yàn)證類(lèi)的封裝與使用方法詳解

PHP驗(yàn)證類(lèi)的封裝與使用方法詳解

熱門(mén)標(biāo)簽:地圖標(biāo)注怎么做商戶驗(yàn)證 兼職做地圖標(biāo)注好賺錢(qián)嗎 打開(kāi)百度地圖標(biāo)注 海南外呼系統(tǒng)方案 400 電話 辦理 智能電銷(xiāo)語(yǔ)音機(jī)器人資訊 蘇州外呼系統(tǒng)有效果嗎 亳州企業(yè)外呼系統(tǒng) 山東電銷(xiāo)卡外呼系統(tǒng)原理是什么

本文實(shí)例講述了PHP驗(yàn)證類(lèi)的封裝與使用方法。分享給大家供大家參考,具體如下:

?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ù)長(zhǎng)度不能超過(guò)最大長(zhǎng)度',
  'min_len' => '參數(shù)長(zhǎng)度不能小于最小長(zhǎng)度',
  '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è)類(lèi)唯一的亮點(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();
}

勇敢的封裝,利國(guó)利民。

繼續(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ù)長(zhǎng)度不能超過(guò)最大長(zhǎng)度',
  'min_len' => '參數(shù)長(zhǎng)度不能小于最小長(zhǎng)度',
  '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;
  }
 }
 /**
  * 簡(jiǎn)寫(xiě)
  * @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ù)長(zhǎng)度不能超過(guò)最大長(zhǎng)度'.$param,
   'min_len' => '參數(shù)長(zhǎng)度不能小于最小長(zhǎng)度'.$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;
  }
 }
 /**
  * 簡(jiǎn)寫(xiě)
  * @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á)式在線測(cè)試工具:
http://tools.jb51.net/regex/javascript

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

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php正則表達(dá)式用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

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

您可能感興趣的文章:
  • PHP代碼實(shí)現(xiàn)表單數(shù)據(jù)驗(yàn)證類(lèi)
  • PHP 基于文件頭的文件類(lèi)型驗(yàn)證類(lèi)函數(shù)
  • php封裝的表單驗(yàn)證類(lèi)完整實(shí)例
  • php實(shí)現(xiàn)通用的信用卡驗(yàn)證類(lèi)
  • php可擴(kuò)展的驗(yàn)證類(lèi)實(shí)例(可對(duì)郵件、手機(jī)號(hào)、URL等驗(yàn)證)
  • 學(xué)習(xí)thinkphp5.0驗(yàn)證類(lèi)使用方法
  • php常用表單驗(yàn)證類(lèi)用法實(shí)例
  • php編寫(xiě)的一個(gè)E-mail驗(yàn)證類(lèi)

標(biāo)簽:溫州 安康 呼倫貝爾 萊蕪 金華 清遠(yuǎn) 紹興 綏化

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP驗(yàn)證類(lèi)的封裝與使用方法詳解》,本文關(guān)鍵詞  PHP,驗(yàn)證,類(lèi),的,封裝,與,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP驗(yàn)證類(lèi)的封裝與使用方法詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于PHP驗(yàn)證類(lèi)的封裝與使用方法詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    葫芦岛市| 靖安县| 中宁县| 来安县| 泸水县| 垣曲县| 中西区| 盖州市| 阿瓦提县| 丰都县| 宁乡县| 葫芦岛市| 新营市| 包头市| 兴海县| 庄河市| 宿松县| 天全县| 东丽区| 大埔县| 礼泉县| 静海县| 东海县| 尼木县| 息烽县| 伊川县| 江山市| 临猗县| 遵义县| 万载县| 临漳县| 固安县| 涪陵区| 南汇区| 安达市| 拜城县| 望奎县| 璧山县| 米脂县| 两当县| 子长县|