亲宝软件园·资讯

展开

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

TBHacker 人气:0

本文实例讲述了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
/**
 * 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;
  }
 }
}

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.softyun.net/regex/javascript

正则表达式在线生成工具:
http://tools.softyun.net/regex/create_reg

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

加载全部内容

相关教程
猜你喜欢
用户评论