Php regular verification class and regular expression. Frequently used php verification classes and regular expressions will be constantly updated when new ones are encountered; classValidation {privatestaticfunctiongetRexp ($ rexp) php frequently used verification classes and regular expressions
Regular expressions will be updated continuously when new expressions are encountered.
Include "ValidateParameterConfig. php ";
Class Validation
{
Private static function getRexp ($ rexp)
{
$ _ Rexp = array (
'Letter _ number' => '/^ [0-9A-Za-z] + $/', // only uppercase/lowercase letters and numbers are included
'Account' => '/^ [0-9A-Za-z _] + $/', // only uppercase and lowercase letters and numbers are supported.
'Kids' => '/^ [0-9] + (\, [0-9] +) * $/', // verify multiple ids ', 'split type, for example, '1, 2, 3, 4, 5'
'Number' => '/^ [0-9] + $/', // only numbers are allowed
'Personal _ card' => '/^ [0-9A-Za-z] + $/', // ID card
'Email '=>'/^ ([a-zA-Z0-9 _\. \-]) + \ @ ([a-zA-Z0-9 \-]) + \.) + ([a-zA-Z0-9] {2, 4}) + $/', // mailbox
'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] | 1 \ d | 2 [0-8]) | (19 | 20) ([13579] [26] | [2468] [048] | 0 [48]) | (2000)-0? 2-29) $/'// date, including the leap year
);
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 ('e-mail '), $ data) = false ){
Return false;
}
Return true;
}
Class ValidateParameterConfig
{
Public static function getConfig ($ key)
{
// 'Allow _ exist' => true or allow_exist does not exist, indicating that this parameter is required
$ _ 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;
}
}
}
Example:
The parameter sent from the application is
$ Arr = array ('letter _ number' => 'abc123', 'account' => 'acb1234 _');
// Parameter verification
$ _ Msg = Validation: validate ($ arr, 'test ');
If ($ _ msg! = Null ){
Return $ _ msg. 'is invalid parameter ';
}
Http://www.bkjia.com/PHPjc/820421.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/820421.htmlTechArticlephp common Validation classes and regular expressions will be constantly updated when new ones are encountered include ValidateParameterConfig. php; class Validation {private static function getRexp ($ rexp )...