PHP Development Tips (12)-Example code for common PHP regular expressions

Source: Internet
Author: User
Tags email string php regular expression
Regular Expressionsis a logical formula for string manipulation, which is to make a "rule string" that is used to express a filter logic for a string by using a predefined set of specific characters and combinations of those specific characters. Today, this blog post is for some common PHP regular expressions to do a collation, convenient later in the development of the form validation, string lookup and so on.

1, matching mobile phone number of regular expression

Match Mobile number rules:
Mobile phone number basic format:
The front three bits are:
Mobile: 134-139 147 150-152 157-159 182 187 188
Unicom: 130-132 155-156 185 186
Telecom: 133 153 180 189
The following eight bits are: 0-9 digits

/**  * @param $mobile string requires verification of the phone number  * @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, matching the regular expression of the mailbox

Match Mailbox Rules:
The basic format of the mailbox is *****@**.**@, which was preceded by a case letter or a number, followed by 0 or more uppercase or lowercase letters or numbers. _-The string @ is preceded by a string of 1 or more uppercase or lowercase letters or numbers. followed by a string of 1 or more uppercase or lowercase letters or 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 phone number Regular expression

Phone number matching phone number rules:
Area code: 3 to 5 bits, most of them are four, Beijing (010) and Shanghai (021) Three, Tibet has some five, can be wrapped in parentheses or not if there is an area code enclosed by parentheses, there can be 0 to 1 spaces between the area code and the number, if the area code is not enclosed by parentheses, Between the area code and the number can have two-bit length or-
Number: 7 to 8 digits for example: (010) 12345678 or (010) 12345678 or 010 12345678 or 010--12345678

/**  * @param $telephone string Fixed number  * @return string  *  /function Pregtelephone ($telephone)  {      $rule = '/^ (\ ((010) | ( 021) | (0\d{3,4})) \)( ?) ([0-9]{7,8})) | ((010|021|0\d{3,4})) ([-]{1,2}) ([0-9]{7,8}) $/a ';      if (Preg_match ($rule, $telephone, $result)) {          return $result;      } else {          Return] does not match! ";      }  }

4. Regular Expressions matching URL URLs

Match URL rules:
Protocol://Domain name (www/m/wap ... ). name. suffix/file path/filename

...../2017030629/165.html

Protocol://Domain name (www/m/wap ... ). name. suffix/file path/filename? parameters

.../view.php?aid=165

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

.../tags.php?/regular expression/1/

Protocol: Optional, composed of uppercase and lowercase letters, the non-write protocol should not exist://, otherwise must exist://
Domain name: must exist, made up of uppercase and lowercase letters
Name: must exist, alphanumeric kanji
Suffix: must exist, uppercase and lowercase letters and. Composition
File path: Optional, composed of uppercase and lowercase letters and numbers
File name: Optional, composed of uppercase and lowercase letters and numbers
Parameter: Optional, existence must start with, that is, there must be a corresponding parameter information at the beginning

/**  * @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, matching the regular expression of the ID number

Match the ID number rule:
15-bit pure or 18-bit pure or 17 digits plus one X

/**  * @param $idcard string ID 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 of matching area zip code

Matching ZIP code rules:
Six digits, first digit cannot be 0

/**  * @param $pos string zip code  * @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 expression matching IP address

Match IP Rules:
**1.**2.**3.**4**1 can be a 1-9, two-bit 01-99, three-bit 001-255**2 and **3 can be one of 0-9, two-bit 00-99, three-bit 000-255**4 can be one of 1-9, two-bit 01-99, Three bits of 001-255 four parameters must be present.

/**  * @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 $result;      } else {          Return] does not match! ";      }  }

8. Regular expressions matching the time format

Match Time rule:
Form can be: year-month-day hours: minutes: seconds, year-month-day hours: minutes, year-month-day: Year: 1 or 2 beginning with four digits, month: 1 digits 1 to 9, 0 or 1 digits, 0 to 1 digits, and 9 to a number at the beginning of the first digit: 1 to 1 in number of days ; 0 or 1 or 2 or 3, the two-digit 0 The first digit is the number of 1 to 9, 1 or 2 when the single digit is 0 to 9, the first digit is 3 or 0 hours: 1 to 0 of the first digit, 9 or 0 of the beginning of the two digits, the digit is 1 to 0, 9, the beginning of the two digits, The digits are 0-3 minutes: 0 to 9 for one digit, 0 to 5 for the first two digits, the digits are 0 to 9, and minutes: 0 to 9 for one digit, and 0 to 5 for the first two digits, and 0 to 9 for everyone.

/**  * @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 under UTF8

/**  * @param $chinese text in string UTF8 format  * @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:

A detailed explanation of the most commonly used regular expressions in PHP

PHP regular filter code for HTML tags, spaces, line breaks

PHP regular substitution method for assigning characters to variables

An analysis of PHP regular expression matching substitution and segmentation function example

Related Article

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.