PHP development skills (12)-examples of common PHP regular expressions

Source: Internet
Author: User
Tags email string
A regular expression is a logical formula for string operations. it uses predefined characters and combinations of these specific characters to form a "rule string ", this "rule string" is used to express a filtering logic for strings. Today, this blog post is to sort out some common PHP regular expressions, so that we can perform form verification and string search in the future. Regular expressionIt is a logical formula for string operations. it uses predefined characters and combinations of these specific characters to form a "rule string ", this "rule string" is used to express a filtering logic for strings. Today, this blog post is to sort out some common PHP regular expressions, so that we can perform form verification and string search in the future.

1. regular expression matching mobile phone number

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.

/*** @ Param $ mobile string the mobile phone number to be verified * @ return string */function pregMobile ($ mobile) {$ rule = "/^ (13 [0-9]) | 147 | (15 [0-35-9]) | 180 | 182 | (18 [5-9]) [0-9] {8} $/A "; if (preg_match ($ rule, $ mobile, $ result) {return $ result;} else {return "does not match! ";}}

2. match the regular expression of the Mailbox

Email matching rules:
The basic mailbox format is *****@**. ** @ it used to start with a uppercase/lowercase letter or number, followed by 0 to multiple uppercase/lowercase letters, numbers, or. _-string @ followed. it is a string of 1 to multiple upper and lower case letters or numbers. followed by 1 to multiple uppercase/lowercase letters, numbers, or..

/*** @ Param $ email string mailbox number * @ return string result */function pregEmail ($ email) {$ rule = '/^ [a-zA-Z0-9] [a-zA-Z0-9. _-] * \ @ [a-zA-Z0-9] + \. [a-zA-Z0-9 \.] + $/A'; if (preg_match ($ rule, $ email, $ result) {return $ result;} else {return "does not match! ";}}

3. match the regular expression of the phone number

Telephone number matching telephone number rules:
Area Code: three to five places, most of which are four places, Beijing (010) and Shanghai (021). There are five places in Tibet, it can be enclosed in brackets or not. if there is a area code enclosed in brackets, there can be 0 to 1 space between the area code and the number. if the area code is not enclosed in brackets, the area code and the number can have two lengths or-
Number: 7 to 8 digits, for example: (010) 12345678, (010) 12345678, 010 12345678, or 010--12345678

/*** @ Param $ telephone string fixed phone number * @ return string */function pregTelephone ($ telephone) {$ rule = '/^ (\ (010) | (021) | (0 \ d {3, 4 }))\)(?) ([0-9] {}) | (010 | 021 | 0 \ d }) ([0-9] {7,8}) $/A'; if (preg_match ($ rule, $ telephone, $ result) {return $ result ;} else {return "does not match! ";}}

4. Regular expressions matching URLs

Match url rules:
Protocol: // domain name (www/m/wap...). name. suffix/file path/file name

..../2017030629/165.html

Protocol: // domain name (www/m/wap...). name. suffix/file path/file name? Parameters

.../view.php?aid=165

Protocol: // domain name (www/m/wap...). name. suffix/file path/file name/parameter

.../Tags. php? /Regular expression/1/

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

/*** @ Param $ url string url address * @ return string result */function pregURL ($ url) {$ rule = '/^ ([a-zA-Z] + )(:\/\/))? ([A-zA-Z] + )\. (\ w + )\. ([\ w.] +) (\/([\ w] + )\/?) * (\/[A-zA-Z0-9] + \. (\ w +) * (\/([\ w] + )\/?) *(\? (\ W + =? [\ W] *) * (&? \ W + =? [\ W] *) * $/'; if (preg_match ($ rule, $ url, $ result) {return $ result;} else {return "does not match! ";}}

5. Regular expressions matching ID card numbers

Rules for matching ID card numbers:
15 pure digits, 18 pure digits, 17 digits, and an x

/*** @ Param $ idcard string ID card number * @ return string result */function pregIdcard ($ idcard) {$ rule = '/^ ([0-9] {15}) | ([0-9] {18 }) | ([0-9] {17} x) $/'; if (preg_match ($ rule, $ idcard, $ result) {return $ result ;} else {return "does not match! ";}}

6. regular expression for matching the region ZIP code

Matching zip code rules:
Six digits. The first digit cannot be 0.

/*** @ Param $ pos string zip code number * @ return string result */function pregPOS ($ pos) {$ rule = '/^ [1-9] \ d {5} $/'; if (preg_match ($ rule, $ pos, $ result )) {return $ result;} else {return "does not match! ";}}

7. Regular expressions matching IP addresses

Matching ip rules:
** 1. ** 2. ** 3. ** 4 ** 1 can be 1-9 of a single digit, 01-99 of two digits, the three-digit 001-255 ** 2 and ** 3 can be one 0-9, and the two-digit 00-99, the three-digit 000-255 ** 4 can be one-digit 1-9, the two-digit 01-99, and the three-digit-parameters must exist.

/*** @ Param $ ip string ip address * @ return string result */function pregIP ($ ip) {$ 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]) $/'; if (preg_match ($ rule, $ ip, $ result) {return $ re Sult;} else {return "does not match! ";}}

8. regular expression matching time format

Matching time rules:
The format can be: year-month-day hour: minute: second, year-month-day hour: minute, year-month-day: Four digits starting with year: 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, the number of digits starting with 1 is the number of days from 1 to 2: The number of digits starting with 1 to 9; the number of digits starting with 0 or 1 or 2 or 3, the number of digits starting with 0 is the number of digits from 1 to 9, the number of digits starting with 1 or 2 is the number from 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, with one digit ranging from 0 to 9; two digits starting with 2, with one digit ranging from 0 to 9; double digits starting with 0 to 5, with a single digit ranging from 0 to 9; minute: one digit starting with 0 to 9; two digits starting with 0 to 5, respectively.

/*** @ Param $ time string time * @ return string result */function pregTime ($ time) {$ 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])? $/'; If (preg_match ($ rule, $ time, $ result) {return $ result;} else {return "does not match! ";}}

9. Regular expressions matching Chinese characters in utf8

/*** @ Param $ chinese string utf8 text * @ return string result */function pregChinese ($ chinese) {$ rule = '/([\ x {4e00}-\ x {9fa5}]) {1}/U'; if (preg_match ($ rule, $ chinese, $ result) {return $ result;} else {return "does not match! ";}}

Related articles:

Explanation of the most common regular expressions in PHP

Php regular expression filtering code for html tags, spaces, and line breaks

Php regular expression replacement variable specifies the character method

Analysis on matching, replacing, and splitting functions of PHP regular expressions

The above is the PHP development skills (12)-detailed description of the instance code for common PHP regular expressions. For more information, see other related articles in the first PHP community!

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.