php可擴展的驗證類實例(可對郵件、手機號、URL等驗證)
更新時間:2015年07月09日 14:46:51 作者:henosteven
這篇文章主要介紹了php可擴展的驗證類,實例分析了php針對郵件、手機號、URL等常用的驗證技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php可擴展的驗證類。分享給大家供大家參考。具體分析如下:
這里介紹一個可擴展的php驗證類,
類里面可以的各類驗證可自行調(diào)整實現(xiàn),現(xiàn)在為基本實現(xiàn)方式。
需要添加規(guī)則的話, 直接定義方法,方法名即為規(guī)則名稱。具體參考使用方法。
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文件如下:
<?php
/**
* Validator 數(shù)據(jù)驗證類
* @package library
* @category library
* @author Steven
* @version 1.0
*/
/**
* Validator 數(shù)據(jù)驗證類
* @package library
* @category library
* @author Steven
* @version 1.0
*/
class Validator {
/**
* 待校驗數(shù)據(jù)
* @var array
*/
private $_data;
/**
* 校驗規(guī)則
* @var array
*/
private $_ruleList = null;
/**
* 校驗結果
* @var bool
*/
private $_result = null;
/**
* 校驗數(shù)據(jù)信息
* @var array
*/
private $_resultInfo = array();
/**
* 構造函數(shù)
* @param array $data 待校驗數(shù)據(jù)
*/
public function __construct($data = null)
{
if ($data) {
$this->_data = $data;
}
}
/**
* 設置校驗規(guī)則
* @param string $var 帶校驗項key
* @param mixed $rule 校驗規(guī)則
* @return void
*/
public function setRule($var, $rule)
{
$this->_ruleList[$var] = $rule;
}
/**
* 檢驗數(shù)據(jù)
* @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;
/* 如果沒有設置校驗規(guī)則直接返回 true */
if ($this->_ruleList === null || !count($this->_ruleList)) {
return $result;
}
/* 已經(jīng)設置規(guī)則,則對規(guī)則逐條進行校驗 */
foreach ($this->_ruleList as $ruleKey => $ruleItem) {
/* 如果檢驗規(guī)則為單條規(guī)則 */
if (!is_array($ruleItem)) {
$ruleItem = trim($ruleItem);
if (method_exists($this, $ruleItem)) {
/* 校驗數(shù)據(jù),保存校驗結果 */
$tmpResult = $this->$ruleItem($ruleKey);
if (!$tmpResult) {
$this->_resultInfo[$ruleKey][$ruleItem] = $tmpResult;
$result = false;
}
}
continue;
}
/* 校驗規(guī)則為多條 */
foreach ($ruleItem as $ruleItemKey => $rule) {
if (!is_array($rule)) {
$rule = trim($rule);
if (method_exists($this, $rule)) {
/* 校驗數(shù)據(jù),設置結果集 */
$tmpResult = $this->$rule($ruleKey);
if (!$tmpResult) {
$this->_resultInfo[$ruleKey][$rule] = $tmpResult;
$result = false;
}
}
} else {
if (method_exists($this, $ruleItemKey)) {
/* 校驗數(shù)據(jù),設置結果集 */
$tmpResult = $this->$ruleItemKey($ruleKey, $rule);
if (!$tmpResult) {
$this->_resultInfo[$ruleKey][$ruleItemKey] = $tmpResult;
$result = false;
}
}
}
}
}
return $result;
}
/**
* 獲取校驗結果數(shù)據(jù)
* @return [type] [description]
*/
public function getResultInfo()
{
return $this->_resultInfo;
}
/**
* 校驗必填參數(shù)
* @param string $varName 校驗項
* @return bool
*/
public function required($varName)
{
$result = false;
if (is_array($this->_data) && isset($this->_data[$varName])) {
$result = true;
}
return $result;
}
/**
* 校驗參數(shù)長度
*
* @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]+?@[-\w.]+?$/', $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;
}
/**
* 校驗參數(shù)為數(shù)字
* @param string $varName 校驗項
* @return bool
*/
public function digit($varName)
{
$result = false;
if ($this->required($varName) && is_numeric($this->_data[$varName])) {
$result = true;
}
return $result;
}
/**
* 校驗參數(shù)為身份證
* @param string $varName 校驗項
* @return bool
*/
public function ID($ID)
{
}
/**
* 校驗參數(shù)為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]?::)?\w+?(\.\w+?)$/', $url)) {
$result = false;
}
}
return $result;
}
}
?>
希望本文所述對大家的php程序設計有所幫助。
相關文章
在WAMP環(huán)境下搭建ZendDebugger php調(diào)試工具的方法
一直以來,寫php都是用Dreamweaver,有時候真是痛苦啊,出現(xiàn)一個bug,想看一些元素的值,必須用echo輸出來才知道,現(xiàn)在了解到一個斷點調(diào)試利器,php終于也能像java、.NET那樣斷點調(diào)試,這就是——ZendDebugger2011-07-07
php session實現(xiàn)多級目錄存放實現(xiàn)代碼
這篇文章主要介紹了php session實現(xiàn)多級目錄存放實現(xiàn)代碼,需要的朋友可以參考下2016-02-02
PHP中通過ADO調(diào)用Access數(shù)據(jù)庫的方法測試不通過
PHP中通過ADO調(diào)用Access數(shù)據(jù)庫的方法測試不通過...2006-12-12

