Common php regular expression matching

Source: Internet
Author: User
[Php] functionpregPN ($ test) {/** matching mobile phone number rules: basic mobile phone number format: the first three digits are mobile: Unicom: 130-132155-156185186 Telecom: 1331531... "/> <script

[Php]

Function pregPN ($ test ){
/**
Matching mobile phone number
Rules:
Basic mobile phone number format:
The first three are:
Mobile: 134-139 147 150-152 157-159 182 187
China Unicom: 130-132 155-156 185 186
China Telecom: 133 153 180 189
The last eight digits are:
0-9 digits
*/

$ Rule = "/^ (13 [0-9]) | 147 | (15 [0-35-9]) | 180 | 182 | (18 [5-9]) [0-9] {8} $/";
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

Function pregPN ($ test ){
/**
Matching mobile phone number
Rules:
Basic mobile phone number format:
The first three are:
Mobile: 134-139 147 150-152 157-159 182 187
China Unicom: 130-132 155-156 185 186
China Telecom: 133 153 180 189
The last eight digits are:
0-9 digits
*/

$ Rule = "/^ (13 [0-9]) | 147 | (15 [0-35-9]) | 180 | 182 | (18 [5-9]) [0-9] {8} $/";
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

 

[Php]
Function pregE ($ test ){
/**
Matching email
Rules:
The basic mailbox format is *****@**.**
@ It used to start with a uppercase/lowercase letter or number, followed by a string of 0 to multiple uppercase/lowercase letters, numbers, or. _-
@ Followed by. is a string of 1 to multiple upper and lower case letters or numbers
. Followed by a string of 1 to multiple upper and lower case letters, numbers, or.
*/
$ Zhengze = '/^ [a-zA-Z0-9] [a-zA-Z0-9. _-] * \ @ [a-zA-Z0-9] + \. [a-zA-Z0-9 \.] + $/';
Preg_match ($ zhengze, $ test, $ result );
Return $ result;
}

Function pregE ($ test ){
/**
Matching email
Rules:
The basic mailbox format is *****@**.**
@ It used to start with a uppercase/lowercase letter or number, followed by a string of 0 to multiple uppercase/lowercase letters, numbers, or. _-
@ Followed by. is a string of 1 to multiple upper and lower case letters or numbers
. Followed by a string of 1 to multiple upper and lower case letters, numbers, or.
*/
$ Zhengze = '/^ [a-zA-Z0-9] [a-zA-Z0-9. _-] * \ @ [a-zA-Z0-9] + \. [a-zA-Z0-9 \.] + $/';
Preg_match ($ zhengze, $ test, $ result );
Return $ result;
}

 

[Php]
Function pregt( $ test ){
/**
Phone number matching
Telephone number rules:
Area Code: 3 to 5 digits, mostly four digits, Beijing (010) and Shanghai (021). Tibet has five digits, which can be wrapped in brackets or not.
If the area code is enclosed by parentheses, there can be 0 to 1 space between the area code and the number. if the area code is not enclosed by parentheses, there can be two lengths between the area code and the number, or-
Number: 7 to 8 digits
Example: (010) 12345678 or (010) 12345678 or 010 12345678 or 010--12345678
*/
$ Rule = '/^ (\ (010) | (021) | (0 \ d {3, 4 }))\)(?) ([0-9] {}) | (010 | 021 | 0 \ d }) ([0-9] {7,8}) $/';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

Function pregt( $ test ){
/**
Phone number matching
Telephone number rules:
Area Code: 3 to 5 digits, mostly four digits, Beijing (010) and Shanghai (021). Tibet has five digits, which can be wrapped in brackets or not.
If the area code is enclosed by parentheses, there can be 0 to 1 space between the area code and the number. if the area code is not enclosed by parentheses, there can be two lengths between the area code and the number, or-
Number: 7 to 8 digits
Example: (010) 12345678 or (010) 12345678 or 010 12345678 or 010--12345678
*/
$ Rule = '/^ (\ (010) | (021) | (0 \ d {3, 4 }))\)(?) ([0-9] {}) | (010 | 021 | 0 \ d }) ([0-9] {7,8}) $/';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}
 

 

[Php]
Function pregURL ($ test ){
/**
Matching url
Url rules:
Example
Protocol: // domain name (www/tieba/baike...). name. suffix/file path/file name
Http://zhidao.baidu.com/question/535596723.html
Protocol: // domain name (www/tieba/baike...). name. suffix/file path/file name? Parameters
Www.lhrb.com.cn/portal.php? Mod = views & aid = 7412
Protocol: // domain name (www/tieba/baike...). name. suffix/file path/file name/parameter
Http://www.xugou.com.cn/yiji/erji/index.php/canshu/11

Protocol: dispensable, consisting of uppercase and lowercase letters. if the protocol is not written, it should not exist: //; otherwise, it must exist ://
Domain name: must exist, consisting of uppercase and lowercase letters
Name: it must exist. it must contain letters, numbers, and Chinese characters.
Suffix: must exist, consisting of uppercase and lowercase letters and.
File path: optional, consisting of uppercase and lowercase letters and numbers
File name: optional, consisting of uppercase and lowercase letters and numbers
Parameter: optional? Start? Parameters must start
*/
$ Rule = '/^ ([a-zA-Z] + )(:\/\/))? ([A-zA-Z] + )\. (\ w + )\. ([\ w.] +) (\/([\ w] + )\/?) * (\/[A-zA-Z0-9] + \. (\ w +) * (\/([\ w] + )\/?) *(\? (\ W + =? [\ W] *) * (&? \ W + =? [\ W] *) * $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

Function pregURL ($ test ){
/**
Matching url
Url rules:
Example
Protocol: // domain name (www/tieba/baike...). name. suffix/file path/file name
Http://zhidao.baidu.com/question/535596723.html
Protocol: // domain name (www/tieba/baike...). name. suffix/file path/file name? Parameters
Www.lhrb.com.cn/portal.php? Mod = views & aid = 7412
Protocol: // domain name (www/tieba/baike...). name. suffix/file path/file name/parameter
Http://www.xugou.com.cn/yiji/erji/index.php/canshu/11

Protocol: dispensable, consisting of uppercase and lowercase letters. if the protocol is not written, it should not exist: //; otherwise, it must exist ://
Domain name: must exist, consisting of uppercase and lowercase letters
Name: it must exist. it must contain letters, numbers, and Chinese characters.
Suffix: must exist, consisting of uppercase and lowercase letters and.
File path: optional, consisting of uppercase and lowercase letters and numbers
File name: optional, consisting of uppercase and lowercase letters and numbers
Parameter: optional? Start? Parameters must start
*/
$ Rule = '/^ ([a-zA-Z] + )(:\/\/))? ([A-zA-Z] + )\. (\ w + )\. ([\ w.] +) (\/([\ w] + )\/?) * (\/[A-zA-Z0-9] + \. (\ w +) * (\/([\ w] + )\/?) *(\? (\ W + =? [\ W] *) * (&? \ W + =? [\ W] *) * $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

 

[Php]
Function pregIC ($ test ){
/**
Matching ID card number
Rules:
15 pure digits, 18 pure digits, 17 digits, and an x
*/
$ Rule = '/^ ([0-9] {15}) | ([0-9] {18 }) | ([0-9] {17} x) $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

Function pregIC ($ test ){
/**
Matching ID card number
Rules:
15 pure digits, 18 pure digits, 17 digits, and an x
*/
$ Rule = '/^ ([0-9] {15}) | ([0-9] {18 }) | ([0-9] {17} x) $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}
[Php]
Function pregPOS ($ test ){
/**
Matching zip code
Rule: six digits. The first digit cannot be 0.
*/
$ Rule = '/^ [1-9] \ d {5} $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

Function pregPOS ($ test ){
/**
Matching zip code
Rule: six digits. The first digit cannot be 0.
*/
$ Rule = '/^ [1-9] \ d {5} $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

 

[Php]
Function pregIP ($ test ){
/**
Matching ip address
Rules:
** 1. ** 2. ** 3. ** 4
** 1 can be 1-9 of a single digit, 01-99 of two digits, 001-255 of three digits
** 2 and ** 3 can be 0-9 of a single digit, 00-99 of two digits, 000-255 of three digits
** 4 can be 1-9 of one digit, 01-99 of two digits, 001-255 of three digits
Four parameters must exist
*/
$ Rule = '/^ ([1-9]) | (0 [1-9]) | ([1-9] [0-9]) | (00 [1-9]) | (0 [1-9] [0-9]) | (1 [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]) \.) ([0-9] {1, 2}) | ([0-1] [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]) \.) {2} ([1-9]) | (0 [1-9]) | ([1-9] [0-9]) | (00 [1-9]) | (0 [1-9] [0-9]) | (1 [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]) $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

Function pregIP ($ test ){
/**
Matching ip address
Rules:
** 1. ** 2. ** 3. ** 4
** 1 can be 1-9 of a single digit, 01-99 of two digits, 001-255 of three digits
** 2 and ** 3 can be 0-9 of a single digit, 00-99 of two digits, 000-255 of three digits
** 4 can be 1-9 of one digit, 01-99 of two digits, 001-255 of three digits
Four parameters must exist
*/
$ Rule = '/^ ([1-9]) | (0 [1-9]) | ([1-9] [0-9]) | (00 [1-9]) | (0 [1-9] [0-9]) | (1 [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]) \.) ([0-9] {1, 2}) | ([0-1] [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]) \.) {2} ([1-9]) | (0 [1-9]) | ([1-9] [0-9]) | (00 [1-9]) | (0 [1-9] [0-9]) | (1 [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]) $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

 

[Php]
Function pregTI ($ test ){
/**
Matching time
Rules:
The format can be:
Year-month-day hour: minute: Second
Year-month-day hour: minute
Year-Month-Day
Year: four digits starting with 1 or 2
Month: the number of 1-9 digits; the number of two digits starting with 0 or 1. the number of digits starting with 0 is the number of one to nine digits, and the number of digits starting with 1 is the number of one to two digits.
Day: number of 1-9 digits; two digits starting with 0 or 1 or 2 or 3. the number of digits starting with 0 is one to nine digits, the number of digits starting with 1 or 2 is 0 to 9, and the number of digits starting with 3 is 0 or 1.
Hour: one digit between 0 and 9; two digits starting with 0 or 1; one digit ranges from 0 to 9; two digits starting with 2, and one digit ranges from 0 to 3.
Minute: one digit from 0 to 9; two digits starting with 0 to 5; one digit is from 0 to 9;
Minute: one digit from 0 to 9; two digits starting with 0 to 5.
*/
$ Rule = '/^ ([1-2] [0-9] {3}-) ([1-9]) | (0 [1-9]) | (1 [0-2])-) ([1-9]) | (0 [1-9]) | ([1-2] [0-9]) | (3 [0-1]) ([0-9]) | ([0-1] [0-9]) | (2 [0-3]) :( ([0-9]) | ([0-5] [0-9]) ([0-9]) | ([0-5] [0-9])? $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

Function pregTI ($ test ){
/**
Matching time
Rules:
The format can be:
Year-month-day hour: minute: Second
Year-month-day hour: minute
Year-Month-Day
Year: four digits starting with 1 or 2
Month: the number of 1-9 digits; the number of two digits starting with 0 or 1. the number of digits starting with 0 is the number of one to nine digits, and the number of digits starting with 1 is the number of one to two digits.
Day: number of 1-9 digits; two digits starting with 0 or 1 or 2 or 3. the number of digits starting with 0 is one to nine digits, the number of digits starting with 1 or 2 is 0 to 9, and the number of digits starting with 3 is 0 or 1.
Hour: one digit between 0 and 9; two digits starting with 0 or 1; one digit ranges from 0 to 9; two digits starting with 2, and one digit ranges from 0 to 3.
Minute: one digit from 0 to 9; two digits starting with 0 to 5; one digit is from 0 to 9;
Minute: one digit from 0 to 9; two digits starting with 0 to 5.
*/
$ Rule = '/^ ([1-2] [0-9] {3}-) ([1-9]) | (0 [1-9]) | (1 [0-2])-) ([1-9]) | (0 [1-9]) | ([1-2] [0-9]) | (3 [0-1]) ([0-9]) | ([0-1] [0-9]) | (2 [0-3]) :( ([0-9]) | ([0-5] [0-9]) ([0-9]) | ([0-5] [0-9])? $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
}

 

[Php]
Function pregCh ($ test ){
// Match Chinese characters in utf8
$ Rule = '/([\ x {4e00}-\ x {9fa5}]) {1}/U ';
Preg_match_all ($ rule, $ test, $ result );
Return $ result;
}

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.