Home > Backend Development > PHP Tutorial > PHP extensible verification class example (can verify email, mobile phone number, URL, etc.), example url_PHP tutorial

PHP extensible verification class example (can verify email, mobile phone number, URL, etc.), example url_PHP tutorial

WBOY
Release: 2016-07-13 09:46:56
Original
824 people have browsed it

php extensible verification class instance (can verify email, mobile phone number, URL, etc.), instance url

The example in this article describes the php extensible verification class. Share it with everyone for your reference. The specific analysis is as follows:

Here is an extensible PHP verification class,
All kinds of verifications that can be performed in the class can be adjusted and implemented by themselves. This is the basic implementation method.
If you need to add a rule, define the method directly, and the method name is the rule name. Please refer to the usage method for details.

require_once('./Validator.class.php');
$data = array(
  'nickname' => 'heno' ,
  'realname' => 'steven',
  'age' => 25,
  'mobile' => '1521060426');
$validator = new Validator($data);
$validator->setRule('nickname', 'required');
$validator->setRule('realname', array('length' => array(1,6), 'required'));
$validator->setRule('age', array('required', 'digit'));
$validator->setRule('mobile', array('mobile'));
$result = $validator->validate();
var_dump($result);
var_dump($validator->getResultInfo());

Copy after login

The Validator.class.php file is as follows:

<&#63;php
/**
 * Validator 数据验证类
 * @package library
 * @category library
 * @author Steven
 * @version 1.0
 */
/**
 * Validator 数据验证类
 * @package library
 * @category library
 * @author Steven
 * @version 1.0
 */
class Validator {
 /**
  * 待校验数据
  * @var array
  */
 private $_data;
 /**
  * 校验规则
  * @var array
  */
 private $_ruleList = null;
 /**
  * 校验结果
  * @var bool
  */
 private $_result = null;
 /**
  * 校验数据信息
  * @var array
  */
 private $_resultInfo = array();
 /**
  * 构造函数
  * @param array $data 待校验数据
  */
 public function __construct($data = null)
 {
  if ($data) {
   $this->_data = $data;
  }
 }
 /**
  * 设置校验规则
  * @param string $var 带校验项key
  * @param mixed $rule 校验规则
  * @return void
  */
 public function setRule($var, $rule)
 {
  $this->_ruleList[$var] = $rule;
 }
 /**
  * 检验数据
  * @param array $data 
  * <code>
  * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25);
  * $validator = new Validator($data);
  * $validator->setRule('nickname', 'required');
  * $validator->setRule('realname', array('lenght' => array(1,4), 'required'));
  * $validator->setRule('age', array('required', 'digit'));
  * $result = $validator->validate();
  * var_dump($validator->getResultInfo());
  * </code>
  * @return bool
  */
 public function validate($data = null)
 {
  $result = true;
  /* 如果没有设置校验规则直接返回 true */
  if ($this->_ruleList === null || !count($this->_ruleList)) {
   return $result;
  }
  /* 已经设置规则,则对规则逐条进行校验 */
  foreach ($this->_ruleList as $ruleKey => $ruleItem) {
   /* 如果检验规则为单条规则 */
   if (!is_array($ruleItem)) {
    $ruleItem = trim($ruleItem);
    if (method_exists($this, $ruleItem)) {
     /* 校验数据,保存校验结果 */
     $tmpResult = $this->$ruleItem($ruleKey);
     if (!$tmpResult) {
      $this->_resultInfo[$ruleKey][$ruleItem] = $tmpResult;
      $result = false;
     }
    }
    continue;
   }
   /* 校验规则为多条 */
   foreach ($ruleItem as $ruleItemKey => $rule) {
    if (!is_array($rule)) {
     $rule = trim($rule);
     if (method_exists($this, $rule)) {
      /* 校验数据,设置结果集 */
      $tmpResult = $this->$rule($ruleKey);
      if (!$tmpResult) {
       $this->_resultInfo[$ruleKey][$rule] = $tmpResult;
       $result = false;
      }
     }
    } else {
     if (method_exists($this, $ruleItemKey)) {
      /* 校验数据,设置结果集 */
      $tmpResult = $this->$ruleItemKey($ruleKey, $rule);
      if (!$tmpResult) {
       $this->_resultInfo[$ruleKey][$ruleItemKey] = $tmpResult;
       $result = false;
      }
     }
    }
   }
  }
  return $result;
 }
 /**
  * 获取校验结果数据
  * @return [type] [description]
  */
 public function getResultInfo()
 {
  return $this->_resultInfo;
 }
 /**
  * 校验必填参数
  * @param string $varName 校验项
  * @return bool
  */
 public function required($varName) 
 {
  $result = false;
  if (is_array($this->_data) && isset($this->_data[$varName])) {
   $result = true;
  }
  return $result;
 }
 /**
  * 校验参数长度
  * 
  * @param string $varName 校验项
  * @param array $lengthData array($minLen, $maxLen)
  * @return bool
  */
 public function length($varName, $lengthData)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $varLen = mb_strlen($this->_data[$varName]);
   $minLen = $lengthData[0];
   $maxLen = $lengthData[1];
   if ($varLen < $minLen || $varLen > $maxLen) {
    $result = true;
   }
  }
  return $result;
 }
 /**
  * 校验邮件
  * @param string $varName 校验项
  * @return bool
  */
 public function email($varName)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $email = trim($this->_data[$varName]);
   if (preg_match('/^[-\w]+&#63;@[-\w.]+&#63;$/', $email)) {
    $result = false;
   }
  }
  return $result;
 }
 /**
  * 校验手机
  * @param string $varName 校验项
  * @return bool
  */
 public function mobile($varName)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $mobile = trim($this->_data[$varName]);
   if (!preg_match('/^1[3458]\d{10}$/', $mobile)) {
    $result = false;
   }
  }
  return $result;
 }
 /**
  * 校验参数为数字
  * @param string $varName 校验项
  * @return bool
  */
 public function digit($varName)
 {
  $result = false;
  if ($this->required($varName) && is_numeric($this->_data[$varName])) {
   $result = true;
  }
  return $result;
 }
 /**
  * 校验参数为身份证
  * @param string $varName 校验项
  * @return bool
  */
 public function ID($ID)
 {
 }
 /**
  * 校验参数为URL
  * @param string $varName 校验项
  * @return bool
  */
 public function url($url)
 {
  $result = true;
  /* 如果该项没有设置,默认为校验通过 */
  if ($this->required($varName)) {
   $url = trim($this->_data[$varName]);
   if(!preg_match('/^(http[s]&#63;::)&#63;\w+&#63;(\.\w+&#63;)$/', $url)) {
    $result = false;
   }
  }
  return $result;
 }
}
&#63;>

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1029592.htmlTechArticlephp extensible verification class instance (can verify email, mobile phone number, URL, etc.), instance url Example of this article Describes PHP's extensible verification class. Share it with everyone for your reference. The specific analysis is as follows...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template