PHP Common regular Match _php tutorial

Source: Internet
Author: User
[PHP]

function Pregpn ($test) {
/**
Match phone 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-digit number
*/

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

function Pregpn ($test) {
/**
Match phone 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-digit number
*/

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

[PHP]
function Prege ($test) {
/**
Match mailbox
Rules:
The basic format of the mailbox is *****@**.**
@ is preceded by a case letter or a number, followed by 0 or more uppercase or lowercase letters or numbers. _-The string
After @ To. A string of 1 or more uppercase or lowercase letters or numbers
. followed by a string of 1 to more uppercase or lowercase letters or numbers or.
*/
$zhengze = '/^[a-za-z0-9][a-za-z0-9._-]*\@[a-za-z0-9]+\. [A-za-z0-9\.] +$/A ';
Preg_match ($zhengze, $test, $result);
return $result;
}

function Prege ($test) {
/**
Match mailbox
Rules:
The basic format of the mailbox is *****@**.**
@ is preceded by a case letter or a number, followed by 0 or more uppercase or lowercase letters or numbers. _-The string
After @ To. A string of 1 or more uppercase or lowercase letters or numbers
. followed by a string of 1 to more uppercase or lowercase letters or numbers or.
*/
$zhengze = '/^[a-za-z0-9][a-za-z0-9._-]*\@[a-za-z0-9]+\. [A-za-z0-9\.] +$/A ';
Preg_match ($zhengze, $test, $result);
return $result;
}

[PHP]
function Pregtp ($test) {
/**
Phone number Matching
Phone number rules:
Area code: 3 to 5, most of them are four, Beijing (010) and Shanghai (021) Three, Tibet has some five, can be wrapped in parentheses can also not
If there is an area code enclosed by parentheses, there can be between 0 and 1 spaces between the area code and the number, and if the area code is not enclosed by parentheses, the area code and number can have two-bit lengths or-
Number: 7 to 8 digits
For example: (010) 12345678 or (010) 12345678 or 010 12345678 or 010--12345678
*/
$rule = '/^ (\ ((010) | ( 021) | (0\d{3,4})) \)( ?) ([0-9]{7,8})) | ((010|021|0\d{3,4})) ([-]{1,2}) ([0-9]{7,8}) $/a ';
Preg_match ($rule, $test, $result);
return $result;
}

function Pregtp ($test) {
/**
Phone number Matching
Phone number rules:
Area code: 3 to 5, most of them are four, Beijing (010) and Shanghai (021) Three, Tibet has some five, can be wrapped in parentheses can also not
If there is an area code enclosed by parentheses, there can be between 0 and 1 spaces between the area code and the number, and if the area code is not enclosed by parentheses, the area code and number can have two-bit lengths or-
Number: 7 to 8 digits
For example: (010) 12345678 or (010) 12345678 or 010 12345678 or 010--12345678
*/
$rule = '/^ (\ ((010) | ( 021) | (0\d{3,4})) \)( ?) ([0-9]{7,8})) | ((010|021|0\d{3,4})) ([-]{1,2}) ([0-9]{7,8}) $/a ';
Preg_match ($rule, $test, $result);
return $result;
}

[PHP]
function Pregurl ($test) {
/**
match url
URL rule:
Example
Protocol://Domain name (www/tieba/baike ... ). name. suffix/file path/filename
http://zhidao.baidu.com/question/535596723.html
Protocol://Domain name (www/tieba/baike ... ). name. suffix/file path/filename? parameter
www.lhrb.com.cn/portal.php?mod=view&aid=7412
Protocol://Domain name (www/tieba/baike ... ). name. suffix/file path/filename/parameter
HTTP://WWW.XUGOU.COM.CN/YIJI/ERJI/INDEX.PHP/CANSHU/11

Protocol: Optional, composed of uppercase and lowercase letters, no write protocol should not exist ://, otherwise must exist://
Domain name: must exist, consist of uppercase and lowercase letters
Name: must exist, alphanumeric kanji
Suffix: must exist, uppercase and lowercase letters and. Make up
file path: Optional, consists of uppercase and lowercase characters
FileName: optional, consists of uppercase and lowercase letters and numbers
Parameters: dispensable, existence must start with, that is, there must be a corresponding parameter information
*/
$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) {
/**
Match URL
URL Rules:
Cases
Protocol://Domain name (www/tieba/baike ... ). name. suffix/file path/filename
Http://zhidao.baidu.com/question/535596723.html
Protocol://Domain name (www/tieba/baike ... ). name. suffix/file path/filename? parameters
www.lhrb.com.cn/portal.php?mod=view&aid=7412
Protocol://Domain name (www/tieba/baike ... ). name. suffix/file path/filename/parameter
Http://www.xugou.com.cn/yiji/erji/index.php/canshu/11

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
*/
$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) {
/**
Match ID number
Rules:
15-bit pure or 18-bit pure or 17 digits plus one X
*/
$rule = '/^ (([0-9]{15}) | ( [0-9] {18}) | ([0-9]{17}x)) $/';
Preg_match ($rule, $test, $result);
return $result;
}

function Pregic ($test) {
/**
Match ID number
Rules:
15-bit pure or 18-bit pure or 17 digits plus one 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, 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, first digit cannot be 0
*/
$rule = '/^[1-9]\d{5}$/';
Preg_match ($rule, $test, $result);
return $result;
}

[PHP]
function Pregip ($test) {
/**
matches ip
rule:
**1.**2.**3.**4
**1 can be one of 1-9, two bits of 01-99, The 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-bit 001-255
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
Rules:
**1.**2.**3.**4
**1 can be one of 1-9, two bits of 01-99, three bits of 001-255
**2 and **3 can be one of 0-9, two bits of 00-99, three bits of 000-255
**4 can be one of 1-9, two bits of 01-99, three bits of 001-255
Four parameters must be present
*/
$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) {
/**
Match time
rule:
The form can be:
year-month-day hour: minutes: Seconds
Year-month-day hour: minute
year-month-day
Year : 1 or 2 begins with four digits
Month: 1 bits 1 to 9; 0 or 1 The beginning of the two digits, 0 when the first digit is 1 to 9 of the number, 1 when the first digit is 1 to 2 of the number of
days: 1 bits 1 to 9, 0 or 1 or 2 or 3, the first digit is 0 to 1 of the number of digits, 1 or 2 at the beginning of the single digit is 0 to 9 of the number, 3 at the beginning of the single digit is 0 or 1
Hours: 0 to 9 of the first digit, 0 or 1 of the beginning of the two digits, the digits are 0 to 9, 2 the beginning of the two digits, the digit is 0-3
minutes: 0 to 9 of the first digit, 0 to 5, the number of digits 0 to 9;
Minutes: 0 to 9 for a single digit; 0 to 5 for two digits, guys 0 to 9
/
$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) {
/**
Match time
Rules:
The form can be:
Year-month-day hour: minutes: seconds
Year-month-day hour: minutes
Year-month-day
Year: 1 or 2 starts with four digits
Month: 1 digits 1 to 9, 0 or 1, the first digit is 0 to 1, and 9 to 1 at the beginning of the 1-digit number.
Days: 1 digits from 1 to 9, 0 or 1 or 2 or 3, and 0 or 1 at the beginning of the first digit, 9 to 1 digits at the beginning of 2 or 0, and 9 to 3 for the first digit
Hours: 0 to 9 for one digit, 0 or 1 for two digits, digits 0 to 9, 2 for two digits, and 0-3 for digits
Minutes: 0 to 9 for one digit, 0 to 5 for two digits, and 0 to 9 for each digit;
Minutes: 0 to 9 for one digit, 0 to 5 for two digits, and 0 to 9 for everyone.
*/
$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) {
UTF8 next to Chinese
$rule = '/([\x{4e00}-\x{9fa5}]) {1}/u ';
Preg_match_all ($rule, $test, $result);
return $result;
}

http://www.bkjia.com/PHPjc/477465.html www.bkjia.com true http://www.bkjia.com/PHPjc/477465.html techarticle [PHP] function PREGPN ($test) {/** match mobile number rules: Mobile phone number basic format: Front three bits: Mobile: 134-139 147 150-152 157-159 182 187 188 Unicom: 130-132 15 5-...

  • 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.