栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php可扩展的验证类实例(可对邮件、手机号、URL等验证)

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php可扩展的验证类实例(可对邮件、手机号、URL等验证)

本文实例讲述了php可扩展的验证类。分享给大家供大家参考。具体分析如下:

这里介绍一个可扩展的php验证类,
类里面可以的各类验证可自行调整实现,现在为基本实现方式。
需要添加规则的话, 直接定义方法,方法名即为规则名称。具体参考使用方法。

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());

Validator.class.php文件如下:

_data = $data;
  }
 }
 
 public function setRule($var, $rule)
 {
  $this->_ruleList[$var] = $rule;
 }
 
 public function validate($data = null)
 {
  $result = 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;
 }
 
 public function getResultInfo()
 {
  return $this->_resultInfo;
 }
 
 public function required($varName) 
 {
  $result = false;
  if (is_array($this->_data) && isset($this->_data[$varName])) {
   $result = true;
  }
  return $result;
 }
 
 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;
 }
 
 public function email($varName)
 {
  $result = true;
  
  if ($this->required($varName)) {
   $email = trim($this->_data[$varName]);
   if (preg_match('/^[-w]+?@[-w.]+?$/', $email)) {
    $result = false;
   }
  }
  return $result;
 }
 
 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;
 }
 
 public function digit($varName)
 {
  $result = false;
  if ($this->required($varName) && is_numeric($this->_data[$varName])) {
   $result = true;
  }
  return $result;
 }
 
 public function ID($ID)
 {
 }
 
 public function url($url)
 {
  $result = true;
  
  if ($this->required($varName)) {
   $url = trim($this->_data[$varName]);
   if(!preg_match('/^(http[s]?::)?w+?(.w+?)$/', $url)) {
    $result = false;
   }
  }
  return $result;
 }
}
?>

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/45083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号