Php extensible verification instance (which can be verified by email, mobile phone number, URL, etc.) and instance url. Php extensible verification instances (which can be verified by email, mobile phone number, URL, etc.). instance url this article describes the php extensible verification classes. Share it with you for your reference. The following is a detailed analysis of php scalable verification instance (which can be verified by email, mobile phone number, URL, etc.) and instance url.
This article describes the php extension verification classes. Share it with you for your reference. The specific analysis is as follows:
An Extensible php verification class is introduced here,
You can adjust the implementation of various types of verification in the class. now it is the basic implementation method.
To add a rule, define the method directly. the method name is the rule name. For details, refer to usage.
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());
The Validator. class. php file is as follows:
<? Php/*** Validator data verification class * @ package library * @ category library * @ author Steven * @ version 1.0 * // *** Validator data verification class * @ package library * @ category library * @ author Steven * @ version 1.0 */class Validator {/*** data to be verified * @ var array */private $ _ data; /*** check rule ** @ var array */private $ _ ruleList = null;/*** check result ** @ var bool */private $ _ result = null; /*** verify data information ** @ var array */private $ _ resultInfo = array (); /*** constructor * @ param array $ data to be verified */public function _ construct ($ data = null) {if ($ data) {$ this-> _ data = $ data ;}} /*** set verification rules ** @ param string $ var with verification item key * @ param mixed $ rule verification rule * @ return void */public function setRule ($ var, $ rule) {$ this-> _ ruleList [$ var] = $ rule;}/*** validate 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; /* Returns true if no validation rule is set */if ($ this-> _ ruleList === null |! Count ($ this-> _ ruleList) {return $ result;}/* rules set, verify the rules one by one */foreach ($ this-> _ ruleList as $ ruleKey => $ ruleItem) {/* if the validation rule is a single rule */if (! Is_array ($ ruleItem) {$ ruleItem = trim ($ ruleItem); if (method_exists ($ this, $ ruleItem) {/* verify data, save the verification result */$ tmpResult = $ this-> $ ruleItem ($ ruleKey); if (! $ TmpResult) {$ this-> _ resultInfo [$ ruleKey] [$ ruleItem] = $ tmpResult; $ result = false ;}} continue ;} /* Check multiple rules */foreach ($ ruleItem as $ ruleItemKey => $ rule) {if (! Is_array ($ rule) {$ rule = trim ($ rule); if (method_exists ($ this, $ rule) {/* check data, set result set */$ tmpResult = $ this-> $ rule ($ ruleKey); if (! $ TmpResult) {$ this-> _ resultInfo [$ ruleKey] [$ rule] = $ tmpResult; $ result = false ;}} else {if (method_exists ($ this, $ ruleItemKey) {/* check the data and set the result set */$ tmpResult = $ this-> $ ruleItemKey ($ ruleKey, $ rule); if (! $ TmpResult) {$ this-> _ resultInfo [$ ruleKey] [$ ruleItemKey] = $ tmpResult; $ result = false ;}}} return $ result ;} /*** obtain verification result data * @ return [type] [description] */public function getResultInfo () {return $ this-> _ resultInfo ;} /*** required parameter for verification * @ param string $ varName verification item * @ return bool */public function required ($ varName) {$ result = false; if (is_array ($ this-> _ data) & isset ($ this-> _ data [$ varName])) {$ Result = true;} return $ result;}/*** check parameter length ** @ param string $ varName check item * @ param array $ lengthData array ($ minLen, $ maxLen) * @ return bool */public function length ($ varName, $ lengthData) {$ result = true;/* if this item is not set, by default, it is verified by */if ($ this-> required ($ varName) {$ varLen = mb_strlen ($ this-> _ data [$ varName]); $ minLen = $ lengthData [0]; $ maxLen = $ lengthData [1]; if ($ varLen <$ minLen | $ varL En> $ maxLen) {$ result = true;} return $ result ;} /*** check email ** @ param string $ varName check item * @ return bool */public function email ($ varName) {$ result = true;/* if this item is not set, by default, it is verified by */if ($ this-> required ($ varName) {$ email = trim ($ this-> _ data [$ varName]); if (preg_match ('/^ [-\ w] +? @ [-\ W.] +? $/', $ Email) {$ result = false ;}} return $ result ;} /*** verify mobile phone ** @ param string $ varName verification item * @ return bool */public function mobile ($ varName) {$ result = true;/* if this item is not set, by default, it is verified by */if ($ this-> required ($ varName) {$ mobile = trim ($ this-> _ data [$ varName]); if (! Preg_match ('/^ 1 [3458] \ d {10} $/', $ mobile) {$ result = false ;}} return $ result ;} /*** the verification parameter is a number * @ param string $ varName verification item * @ return bool */public function digit ($ varName) {$ result = false; if ($ this-> required ($ varName) & is_numeric ($ this-> _ data [$ varName]) {$ result = true;} return $ result ;} /*** check parameter: ID card * @ param string $ varName check item * @ return bool */public function ID ($ ID) {}/ *** check parameter The number is URL * @ param string $ varName validation item * @ return bool */public function url ($ url) {$ result = true;/* if this item is not set, by default, it is verified by */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 will help you with php programming.
Token (which can be verified by email, mobile phone number, URL, etc.). The instance url in this article describes the php scalable verification class. Share it with you for your reference. The specific analysis is as follows...