PHP Extensible Validation Class instance (for verification of mail, phone number, URL, etc.), example url_php tutorial

Source: Internet
Author: User

PHP Extensible Authentication Class instance (can authenticate to mail, phone number, URL, etc.), instance URL


The examples in this article describe PHP's extensible validation classes. Share to everyone for your reference. The specific analysis is as follows:

This article introduces an extensible PHP validation class,
Class can be a variety of validation can be self-tuning implementation, now the basic implementation.
If you need to add a rule, define the method directly, and the method name is the rule name. Refer to the use method for specific reference.

Require_once ('./validator.class.php '); $data = Array (  ' nickname ' = ' Heno ',  ' realname ' = ' Steven '),  ' age ',  ' 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 ());

The Validator.class.php file is as follows:

<?php/** * Validator Data validation class * @package Library * @category Library * @author Steven * @version 1.0 *//** * Validator Data verification  Certificate class * @package Library * @category Library * @author Steven * @version 1.0 */class Validator {/** * to verify data * @var array */Private $_data; /** * Check rule * @var array */private $_rulelist = null; /** * Check Results * @var BOOL */private $_result = null; /** * Verify data information * @var array */private $_resultinfo = Array (); /** * constructor * @param array $data pending data */Public function __construct ($data = null) {if ($data) {$this->_data =  $data; }}/** * Set the check rule * @param string $var with Check key * @param mixed $rule Check rule * @return void */Public function Setrule ($va  R, $rule) {$this->_rulelist[$var] = $rule;}/** * Test data * @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;  /* If no validation rules are set directly return True */if ($this->_rulelist = = = NULL | |!count ($this->_rulelist)) {return $result; }/* Rules are set, the rules are checked individually/foreach ($this->_rulelist as $ruleKey + = $ruleItem) {/* If the test rule is a single rule */if (!is_arra    Y ($ruleItem)) {$ruleItem = Trim ($ruleItem);     if (Method_exists ($this, $ruleItem)) {/* checksum data, save check result */$tmpResult = $this $ruleItem ($ruleKey);      if (! $tmpResult) {$this->_resultinfo[$ruleKey] [$ruleItem] = $tmpResult;     $result = false;   }} continue;     }/* Check rule is multiple */foreach ($ruleItem as $ruleItemKey + = $rule) {if (!is_array ($rule)) {$rule = Trim ($rule);      if (Method_exists ($this, $rule)) {/* checksum data, set result set */$tmpResult = $this-$rule ($ruleKey);       if (! $tmpResult) {$this->_resultinfo[$ruleKey] [$rule] = $tmpResult;      $result = false; }}} and else {if (method_exists ($this, $rulEitemkey)) {/* checksum data, set result set */$tmpResult = $this $ruleItemKey ($ruleKey, $rule);       if (! $tmpResult) {$this->_resultinfo[$ruleKey] [$ruleItemKey] = $tmpResult;      $result = false; }}}}} return $result; }/** * Get checksum result data * @return [type] [description] */Public Function Getresultinfo () {return $this->_resultinfo;}/  * * Check Required parameters * @param string $varName Check item * @return BOOL */Public function required ($varName) {$result = false;  if (Is_array ($this->_data) && isset ($this->_data[$varName])) {$result = true; } return $result; }/** * Verify parameter length * * @param string $varName Check item * @param array $lengthData Array ($minLen, $maxLen) * @return BOOL */  Public function Length ($varName, $lengthData) {$result = true;   /* If the item is not set, the default is check through */if ($this->required ($varName)) {$varLen = Mb_strlen ($this->_data[$varName]);   $minLen = $lengthData [0];   $maxLen = $lengthData [1]; if ($varLen < $minLen | | $varLen &GT   $maxLen) {$result = true; }} return $result;  }/** * Check message * @param string $varName Check item * @return BOOL */Public Function Email ($varName) {$result = true;   /* If the item is not set, the default is check through */if ($this->required ($varName)) {$email = Trim ($this->_data[$varName]); if (Preg_match ('/^[-\w]+?@[-\w.   +?$/', $email)) {$result = false; }} return $result;  }/** * Check phone * @param string $varName Check item * @return BOOL */Public Function Mobile ($varName) {$result = true;   /* If the item is not set, the default is check through */if ($this->required ($varName)) {$mobile = Trim ($this->_data[$varName]);   if (!preg_match ('/^1[3458]\d{10}$/', $mobile)) {$result = false; }} return $result;  The/** * Checksum parameter is a number * @param string $varName Check item * @return BOOL */Public function digit ($varName) {$result = false;  if ($this->required ($varName) && is_numeric ($this->_data[$varName])) {$result = true; } return $result; }/** * Check parameter is ID * @param string $varName Check item * @return BOOL */ Public Function ID ($ID) {}/** * checksum parameter is URL * @param string $varName Check item * @return BOOL */Public Function URL ($url) {  $result = true;   /* If the item is not set, the default is check through */if ($this->required ($varName)) {$url = Trim ($this->_data[$varName]); if (!preg_match ('/^ (http[s]?::)? \w+? ( \.\w+?)   $/', $url)) {$result = false; }} return $result; }}?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1029592.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029592.html techarticle PHP Extensible Validation Class instance (can verify mail, phone number, URL, etc.), instance URL This article describes PHP's extensible validation classes. Share to everyone for your reference. The specific analysis is as follows ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.