Collection and supplement of common PHP regular expressions

Source: Internet
Author: User
Tags php code trim zip

Regular expressions have always been a headache for me, but regular expressions are inseparable in my work. Regular expressions are widely used in code, vim editor, awk, and other Linux commands. This is a common regular expression in PHP code I have collected and tested. First, create a test function:

The code is as follows: Copy code

Function regTest ($ pattern, $ str ){
Var_dump (preg_match ($ pattern, $ str ));
Preg_match_all ($ pattern, $ str, $ matches );
Var_dump ($ matches );
Var_dump (preg_replace ($ pattern, $ str, 'test '));
}

1. Match Chinese characters

The code is as follows: Copy code

$ Pattern = "/[x {4e00}-x {9fa5}]/u ";
$ Str = "-feiyan ";
RegTest ($ pattern, $ str );
// Output results in sequence
// Int (1)
// Array (1) {[0] => array (2) {[0] => string (3) "fei" [1] => string (3) "Success "}}
// String (28) "TestTest-feiyan"

On the Chinese character matching, many online to the "[u4e00-u9fa5]", this regular expression is not necessarily completely correct.

2. Match tab indentation, spaces, and line breaks

The code is as follows: Copy code

$ Pattern = "/[x {4e00}-x {9fa5}]/u ";
$ Str = "-feiyan ";
RegTest ($ pattern, $ str );
// Output results in sequence
// Int (1)
// Array (1) {[0] => array (2) {[0] => string (3) "fei" [1] => string (3) "Success "}}
// String (28) "TestTest-feiyan"
// String (21) "HelloTestTest, TestPHP"

3. Matching Email addresses

The code is as follows: Copy code
$ Pattern = "[w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w +) *] ";
$ Str = 'if you have any questions, contact your service@lenovo.com or contact@ibm.com. ';
RegTest ($ pattern, $ str );
// Output results in sequence
// Int (1)
// Array (4) {[0] => array (2) {[0] => string (18) "service@lenovo.com" [1] => string (15) "contact@ibm.com"} [1] => array (2) {[0] => string (0) "" [1] => string (0) ""} [2] => array (2) {[0] => string (0) "" [1] => string (0) ""} [3] => array (2) {[0] => string (0) "" [1] => string (0 )""}}
// String (41) "If you have any questions, contact Test or Test. "

  
// Use the Filter function
Filter_var ($ email, FILTER_VALIDATE_EMAIL );

Generally, I do not write regular expressions to verify the email address. Using the built-in filter function of PHP can easily verify the email address.

4. Match the Chinese mobile phone number and phone number

The code is as follows: Copy code
// Fixed phone match
$ Pattern = "[d {3, 4}-d {7, 8}]";
$ Str = 'contact number 010-12345678 ';
RegTest ($ pattern, $ str );
  
// The simplest matching method is mobile phone number matching.
$ Pattern = "[1d {10}]";
$ Str = 'contact number 15812345678 ';
RegTest ($ pattern, $ str );

5. Match the image address in HTML

The code is as follows: Copy code

$ Pattern = '/<[img | IMG]. *? Src = ['| "] (. *? (? : 2.16.gif |. jpg]) ['| "]. *? [/]?> /';

$ Str = ' ';

RegTest ($ pattern, $ str );


/**
* Verify the ZIP code
* @ Param string $ value
* @ Param string $ match
* @ Return boolean
*/
Public static function isPostcode ($ value, $ match = '/d {6 }/'){
$ V = trim ($ value );
If (empty ($ v ))
Return false;
Return preg_match ($ match, $ v );
    }
/**
* Verify the IP address
* @ Param string $ value
* @ Param string $ match
* @ Return boolean
*/
Public static function isIP ($ value, $ match = '/^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) $ /'){
$ V = trim ($ value );
If (empty ($ v ))
Return false;
Return preg_match ($ match, $ v );
    }

/**
* Verify the ID card number
* @ Param string $ value
* @ Param string $ match
* @ Return boolean
*/
Public static function isIDcard ($ value, $ match = '/^ d {6} (1 [89]) | (2d) d {2} (0d) | (1 [0-2]) (3 [01]) | ([0-2] d) d {3} (d | X) $/I '){
$ V = trim ($ value );
If (empty ($ v ))
Return false;
Else if (strlen ($ v)> 18)
Return false;
Return preg_match ($ match, $ v );
    }

/**
**
* Verify URLwww.111cn.net
* @ Param string $ value
* @ Param string $ match
* @ Return boolean
*/
Public static function isURL ($ value, $ match = '/^ (http ://)? (Https ://)? ([Wd-] +.) + [w-] + (/[dw -./? % & =] *)? $ /'){
$ V = strtolower (trim ($ value ));
If (empty ($ v ))
Return false;
Return preg_match ($ match, $ v );
    }

Supplement

Form verification match
Verification account, starting with a letter, can be 5-16 bytes, can contain letters and numbers underline: ^ [a-zA-Z] [a-zA-Z0-9 _] {} $

Verification account, cannot be blank, cannot have spaces, can only be English letters: ^ S + [a-z A-Z] $

Account verification. No space is allowed, and no number is allowed: ^ d + $

Verify the user password. It must start with a letter and be in the range of 6-18: ^ [a-zA-Z] w {5, 17} $

Check whether ^ % & ',; =? $ And other characters: [^ % & ',; =? $ X22] +

Matching Email address: w + ([-+.] w +) * @ w + ([-.] w +) *. w + ([-.] w + )*

Match Tencent QQ number: [1-9] [0-9] {4 ,}

The matching date can only be in the format of 2004-10-22: ^ d {4}-d {1, 2}-d {1, 2} $

Match Chinese phone number: ^ d {3}-d {8} | d {4}-d {7, 8} $
Comments: matching forms such as 010-12345678 or 0571-12345678 or 0831-1234567

Match China Zip code: ^ [1-9] d {5 }(?! D) $

Matched ID card: d {14} (d {4} | (d {3} [xX]) | d {1 })
Note: the ID card of China is 15 or 18 characters

Cannot be blank and more than 20 bytes: ^ [s | S] {20,} $

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.