Home  >  Article  >  Backend Development  >  PHP custom validation classes and regular expressions_PHP tutorial

PHP custom validation classes and regular expressions_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:27:05758browse

php common validation classes and regular rules

Regular expressions will be continuously updated when new ones are encountered

include "ValidateParameterConfig.php";

class Validation

 {

private static function getRexp($rexp)

 {

 $_rexp = array (

 'letter_number'=>'/^[0-9A-Za-z]+$/',//Only alphanumeric characters including uppercase and lowercase letters

 'account'=>'/^[0-9A-Za-z_]+$/',//Only alphanumeric underscores include uppercase and lowercase letters

 'ids'=>'/^[0-9]+(,[0-9]+)*$/',//Verify multiple ID types separated by ',' such as '1,2 ,3,4,5'

 'number'=>'/^[0-9]+$/',//Only numbers can be used

 'personal_card'=>'/^[0-9A-Za-z]+$/',//ID card

 'email'=>'/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA- Z0-9]{2,4})+$/',//Email

 'date'=>'/^((((19|20)d{2})-(0?(1|[3-9])|1[012])-(0?[1 -9]|[12]d|30))|(((19|20)d{2})-(0?[13578]|1[02])-31)|(((19|20)d {2})-0?2-(0?[1-9]|1d|2[0-8]))|(((19|20)([13579][26]|[2468][048 ]|0[48]))|(2000))-0?2-29))$/'//Date, including leap years

 );

 if (isset($_rexp[$rexp])) {

return $_rexp[$rexp];

 } else {

return $rexp_not_defind;

 }

 }

Public static function validate($data, $config)

 {

 $_config = ValidateParameterConfig::getConfig($config);

$k = self::allowExist($data, $_config);

 if ($k !== null) {

return $k;

 }

foreach($_config as $k=>$c) {

 if (isset($data[$k]))

 {

 if(isset($c['rexp']))

 {

 if (self::vRexp( $data[$k], $c['rexp']) === false) {

return $k;

 }

 }

 if (isset($c['length']))

 {

 if (self::vLength($data[$k], $c['length']))

 {

return $k;

 }

 }

 if (isset($c['min_length']))

 {

 if (!self::vMinLength($data[$k], $c['min_length']))

 {

return $k;

 }

 }

 if (isset($c['max_length']))

 {

 if (!self::vMaxLength($data[$k], $c['max_length']))

 {

return $k;

 }

 }

 }

 }

return null;

 }

private static function allowExist($data, $config)

 {

foreach ($config as $k=>$v)

 {

 if (!isset($v['allow_exist']) || $v['allow_exist'] == true) {

 if (!isset($data[$k]))

 {

return $k;

 }

 }

 }

return null;

 }

Public static function vRexp($data, $rexp) {

$_rexp = self::getRexp($rexp);

 if (preg_match($_rexp, $data) == false) {

return false;

 }

return true;

 }

public static function vLength($data, $l) {

 if (strlen(trim($data)) == $l) {

return false;

 }

return true;

 }

public static function vMinLength($data, $l) {

 if (strlen(trim($data)) < $l) {

return false;

 }

return true;

 }

public static function vMaxLength($data, $l) {

 if (strlen(trim($data)) > $l) {

return false;

 }

return true;

 }

public static function vLetterNumber($data) {

 if (preg_match(self::getRexp('letter_number'), $data) == false) {

return false;

 }

return true;

 }

public static function vLetterNumber_($data) {

 if (preg_match(self::getRexp('letter_number_'), $data) == false) {

return false;

 }

return true;

 }

public static function vNumber($data) {

 if (preg_match(self::getRexp('number'), $data) == false) {

return false;

 }

return true;

 }

public static function vEmail($data) {

 if (preg_match(self::getRexp('email'), $data) == false) {

return false;

 }

return true;

 }

class ValidateParameterConfig

 {

public static function getConfig ($key)

 {

//'allow_exist'=>true or allow_exist does not exist means that the parameter must be passed

 $_config = array(

'test'=>array(

 'letter_number'=>array('rexp'=>'letter_number', 'allow_exist'=>true),

 'number'=>array('rexp'=>'number', 'min_length'=>1'allow_exist'=>false),

'account'=>array('rexp'=>'account', 'max_length'=>20),

 )

 );

 if (isset($_config[$key])) {

return $_config[$key];

 } else {

return $config_not_defind;

 }

 }

 }

Usage example:

The parameters sent from the application are

$arr = array('letter_number'=>'abc123', 'account'=>'acb1234_');

 //Parameter verification

$_msg = Validation::validate($arr, 'test');

 if ($_msg !== null) {

Return $_msg . ' is invalid parameter';

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/820421.htmlTechArticlephp Common validation classes and regular regular expressions will be continuously updated when new ones are encountered include ValidateParameterConfig.php; class Validation { private static function getRexp($rexp)...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn