PHP implements regular expression verification for the complete helper public class instance, and php verifies the helper instance

Source: Internet
Author: User

PHP implements regular expression verification for the complete helper public class instance, and php verifies the helper instance

This example describes the common Regular Expression verification helper common class implemented by PHP. We will share this with you for your reference. The details are as follows:

Main Code functions:Make up for the project's lack of rigorous verification functions. Regular verification of specific segments, mobile phone number/PHS verification, Valid String Length range verification, email verification, and regular expression verification data.

/***** Regular verification helper public class ***/class CheckForm {// mobile phone number/PHS verification public function Mobile_check ($ mobile, $ type = array ()) {/*** mobile phone number *: 134 [0-8], 135,136,137,138,139,150,151,157,158,159,182,187,188 * Unicom: 130,131,132,152,155,156,185,186 * Telecom: 133,1349, 153,180,189 */$ res [1] = preg_match ('/^ 1 (3 [0-9] | 5 [0-35-9] | 8 [0-9]) \ d {8} $/', $ mobile);/*** China Mobile: China mobile 11*134 [0-8], 135,136,137,138,139,150,151,157, 158,159,182,187,188 */$ res [2] = preg_match ('/^ 1 (34 [0-8] | (3 [5-9] | 5 [017-9] | 8 [0-9]) \ d) \ d {7} $/', $ mobile);/*** China Unicom: china Unicom * 130,131,132,152,155,156,185,186 */$ res [3] = preg_match ('/^ 1 (3 [0-2] | 5 [256] | 8 [56]) \ d {8} $/', $ mobile);/*** China Telecom: China Telecom * 133,1349, 153,180,189 */$ res [4] = preg_match ('/^ 1 (33 | 53 | 8 [09]) [0-9] | 349) \ d {7} $/', $ mobile);/*** fixed line telephone and PHS * area code in mainland China: 010,020,021,022,023,024, 0 25,027,028,029 * Number: seven or eight digits */$ res [5] = preg_match ('/^ 0 (10 | 2 [0-5789] | \ d {3 }) -\ d {7, 8} $/', $ mobile); $ type = empty ($ type )? Array (1, 2, 3, 4, 5): $ type; $ OK = false; foreach ($ type as $ key => $ val) {if ($ res [$ val]) {$ OK = true;} continue;} if ($ mobile & $ OK) {return true;} else {return false ;}} // Valid String Length range verification public function Strlength_check ($ str, $ min = NULL, $ max = NULL) {preg_match_all ("/. /u ", $ str, $ matches); $ len = count ($ matches [0]); if (is_null ($ min )&&! Empty ($ max) & $ len <$ max) {return false;} if (is_null ($ max )&&! Empty ($ min) & $ len> $ min) {return false;} if ($ len <$ min | $ len> $ max) {return false ;} return true;} // verify the public static function isEmail ($ str) {if (! $ Str) {return false;} return preg_match ('# [a-z0-9 & \-_.] + @ [\ w \-_] + ([\ w \-.] + )? \. [\ W \-] + # is ', $ str )? True: false ;} /*** use regular expression to verify data * @ access public * @ param string $ rule verification rule * @ param string $ value the data to be verified * @ return boolean */public function regex ($ rule, $ value) {$ validate = array (// The field is required and cannot be empty. 'require '=>'/\ S + /', // email verification 'email '=>'/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $/', // url verification 'url' =>'/^ http (s ?) :\/\/(?: [A-za-z0-9-] + \.) + [A-za-z] {2, 4 }(? :[\/\? #] [\/= \? % \-&~ '@ [\] \': +! \. # \ W] *)? $/', // Currency verification 'currency' => '/^ \ d + (\. \ d {0, 2 })? $/', // Number verification 'number' =>'/^ [-\ +]? \ D + (\. \ d + )? $/', // Zip verification 'zip' =>'/^ \ d {6} $ /', // integer verification 'integer' => '/^ [-\ +]? \ D + $/', // floating point number verification 'double' =>'/^ [-\ +]? \ D + (\. \ d + )? $/', // English verification 'inc' =>'/^ [A-Za-z] + $/', 'gt0' =>'/^ (?! (0 [0-9] {0, }$) [0-9] {1,} [.] {0,} [0-9] {0,} $ /', // valid account 'account' => '/^ [a-zA-Z] [a-zA-Z0-9 _] {} $ /'); // check whether there is a built-in Regular Expression if (isset ($ validate [strtolower ($ rule)]) $ rule = $ validate [strtolower ($ rule)]; return preg_match ($ rule, $ value) === 1;} function CheckPwd ($ pwd, $ min = NULL, $ max = NULL) {if (strlen ($ pwd)> $ max | strlen ($ pwd) <$ min | preg_match ("/^ \ d * $/", $ pwd) | preg_match ("/^ [a-z] * $/I", $ pwd) {return false ;}return true ;}}

is_null()Checks whether the variable is NULL.

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

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.