php可扩展的验证类实例(可对邮件、手机号、URL等验证)
这篇文章主要介绍了php可扩展的验证类,实例分析了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;
}
}
/**
* 设置校验规则
* @param string $var 带校验项key
* @param mixed $rule 校验规则
* @return void
*/
public function setRule($var, $rule)
{
$this->_ruleList[$var] = $rule;
}
/**
* 检验数据
* @param array $data
*
* $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());
*
* @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 $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;
}
/**
* 校验参数为数字
* @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]?::)?\w+?(\.\w+?)$/', $url)) {
$result = false;
}
}
return $result;
}
}
?>
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
