服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

PHP验证类的封装与使用方法详解

2019-07-04 16:40TBHacker PHP教程

这篇文章主要介绍了PHP验证类的封装与使用方法,涉及php针对邮箱、手机号、字符串相关验证操作封装与使用技巧,需要的朋友可以参考下

本文实例讲述了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' => '参数长度不能超过最大长度',
  'min_len' => '参数长度不能小于最小长度',
  'required' => '缺少参数'
 ];
 // 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] : '参数格式有误';
   }
  }
  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();
}

勇敢的封装,利国利民。

继续封装,支持数组传参。

<?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' => '参数长度不能超过最大长度',
  'min_len' => '参数长度不能小于最小长度',
  'required' => '缺少参数'
 ];
 /**
  * @param $validators array array('email' => 'required|valid_email')
  * @param $input array post数据
  * @return string
  */
 public function is_valid($validators, $input) {
  foreach ($validators as $field => $rules) {
   if (!isset($input[$field]) || empty($input[$field])) {
    self::$error[] = "缺少参数";
   }
   $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] : '参数格式有误';
    }
   }
  }
  if (count(self::$error) == 0) {
   return 0;
  }
  return self::$error[0]; // 返回第一个错误
 }
 /**
  * @param $field string 验证字段
  * @param $rules string 验证规则 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] : '参数格式有误';
   }
  }
  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();
}

PHP验证类的封装与使用方法详解

继续优化!支持错误提示中,添加参数。

<?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数据
  * @return string
  */
 public function is_valid($validators, $input) {
  foreach ($validators as $field => $rules) {
   if (!isset($input[$field]) || empty($input[$field])) {
    self::$error[] = "缺少参数";
   }
   $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 验证规则 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]; // 返回第一个错误
 }
 /**
  * 灵活获取参数
  * @param $rule
  * @param $param
  */
 public static function get_error_tips($rule,$param) {
  $error_tips = [
   'tel' => '手机号格式有误',
   'email' => '邮箱格式有误',
   'max_len' => '参数长度不能超过最大长度'.$param,
   'min_len' => '参数长度不能小于最小长度'.$param,
   'required' => '缺少参数',
   'r' => '缺少参数'
  ];
  return $error_tips[$rule] ? $error_tips[$rule] : '参数格式有误';
 }
 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;
  }
 }
}

PHP验证类的封装与使用方法详解

希望本文所述对大家PHP程序设计有所帮助。

延伸 · 阅读

精彩推荐
  • PHP教程PHP简单判断手机设备的方法

    PHP简单判断手机设备的方法

    这篇文章主要介绍了PHP简单判断手机设备的方法,涉及php基于服务器预定义变量进行判断操作的相关技巧,需要的朋友可以参考下...

    onestopweb2742019-06-20
  • PHP教程PDO::prepare讲解

    PDO::prepare讲解

    今天小编就为大家分享一篇关于PDO::prepare讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    php参考手册3952019-06-26
  • PHP教程php使用fullcalendar日历插件详解

    php使用fullcalendar日历插件详解

    这篇文章主要介绍了php使用fullcalendar日历插件的教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    下页、再停留2922019-06-04
  • PHP教程实例分析PHP将字符串转换成数字的方法

    实例分析PHP将字符串转换成数字的方法

    在本篇文章里我们给大家分享的是关于PHP将字符串转换成数字的方法和相关知识点,有兴趣的朋友们学习下。...

    laozhang3982019-06-27
  • PHP教程细谈php中SQL注入攻击与XSS攻击

    细谈php中SQL注入攻击与XSS攻击

    通常在编程中程序员要考虑的问题不仅是代码效率与代码复用性,而且还要考虑一些安全问题...

    网络1292019-06-19
  • PHP教程PHP PDOStatement::rowCount讲解

    PHP PDOStatement::rowCount讲解

    今天小编就为大家分享一篇关于PHP PDOStatement::rowCount讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    php参考手册1602019-06-23
  • PHP教程PHP5.6读写excel表格文件操作示例

    PHP5.6读写excel表格文件操作示例

    这篇文章主要介绍了PHP5.6读写excel表格文件操作,结合实例形式分析了php5.6环境下使用PHPExcel插件针对Excel表格读写的相关操作技巧,需要的朋友可以参考下...

    Sweet小马2942019-06-09
  • PHP教程PHP单元测试框架PHPUnit用法详解

    PHP单元测试框架PHPUnit用法详解

    这篇文章主要介绍了PHP单元测试框架PHPUnit用法,结合实例形式详细分析了单元测试框架PHPUnit原理、安装、使用相关操作技巧与注意事项,需要的朋友可以参考下...

    一个新手4592019-06-29