Php email address regular expression implementation and explanation _ PHP Tutorial

Source: Internet
Author: User
Tags php email
Php mail address regular expression implementation and detailed description. This article not only describes the regular expression but also describes the composition and usage of mailbox regular expressions. For more information, see, at the same time, we also provide a variety of articles not only about the regular expression, but also about the composition and usage of mailbox regular expressions. if you need to know more, please refer to it, at the same time, we also provide a variety of mailbox verification instances.

Regular expression

The code is as follows:
^ [_. 0-9a-z-] + @ ([0-9a-z] [0-9a-z-] +.) + [a-z] {2, 3} $

In this regular expression, "+" indicates that one or more strings appear consecutively. "^" indicates that the next string must start, "$" indicates that the previous string must appear at the end;
"." Is also ".". here "" is an escape character; "{2, 3}" indicates that the previous string can appear 2-3 times in a row. "()" Indicates that the contained content must appear in the target object at the same time. "[_. "0-9a-z-]" indicates that it is included in "_", ". ","-", letters from a to z, any characters in numbers from 0 to 9;
In this way, the regular expression can be translated as follows:
The following characters must start with (^), "_", and ". ","-", letters from a to z, numbers from 0 to 9 ([_. 0-9a-z-] "," the preceding character must appear at least once (+) ", @," the string starts with a character that contains a letter in the range from a to z and a number in the range from 0 to 9, followed by at least one character in "-", any letter from a to z, any number from 0 to 9, and finally in. end ([0-9a-z] [0-9a-z-] + .)) "," the preceding character appears at least once (+) ", and" Letters From a to z appear 2-3 times, end with it ([a-z] {2, 3} $ )"

The code is as follows:
Function is_valid_email ($ email, $ test_mx = false)
{
If (eregi ("^ ([_ a-z0-9-] + )(. [_ a-z0-9-] +) * @ ([a-z0-9-] + )(. [a-z0-9-] + )*(. [a-z] {2, 4}) [wind_phpcode_0] quot;, $ email ))
If ($ test_mx)
{
List ($ username, $ domain) = split ("@", $ email );
Return getmxrr ($ domain, $ mxrecords );
}
Else
Return true;
Else
Return false;
}

A domain name is composed of a specific character set, English letters, numbers, and "-" (that is, a hyphen or minus sign) of Chinese characters in different countries, but cannot start or end with "-". "-" cannot appear consecutively. The domain name is case-insensitive. The domain name can contain up to 60 bytes (including the suffix. com,. net, and. org ).
/^ [A-z] ([a-z0-9] * [-_]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [.] [a-z] {2, 3} ([.] [a-z] {2 })? $/I;
/Content/I forms a case-insensitive regular expression;
^ Match starts

$ Match ended

[A-z] the e-mail prefix must start with an English letter

([A-z0-9] * [-_]? [A-z0-9] +) * and _ a_2, aaa11, _ 1_a_2 match, and a1 _, aaff_33a _, a _ aa does not match, if it is a null character, but also match, * indicates 0 or more.

* Indicates 0 or more characters.

[A-z0-9] * matches 0 or more English letters or numbers

[-_]? Match 0 or 1 "-" because "-" cannot appear continuously

[A-z0-9] + matches one or more English letters or numbers because '-' cannot end

@ There must be @

([A-z0-9] * [-_]? [A-z0-9] +) + See above ([a-z0-9] * [-_]? [A-z0-9] +) * interpreted, but cannot be empty, + represents one or more.

[.] Use special characters (.) as common characters

[A-z] {2, 3} matches two to three English letters, generally com or net.

([.] [A-z] {2 })? Match 0 or 1 [.] [a-z] {2} (for example. cn, etc.) I do not know whether the last part of .com.cn is generally two. if not, change {2} to {start word count, end word count}

Perfect E-Mail regular expression, with a detailed explanation, please help test it! 2. extract the email from the string:

The code is as follows:
Function getEmail ($ str ){
$ Pattern = "/([a-z0-9] * [-_.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [.] [a-z] {2, 3} ([.] [a-z] {2 })? /I ";
Preg_match_all ($ pattern, $ str, $ emailArr );
Return $ emailArr [0];
}
$ Emailstr = "9999@qq.com.cn I'm not a vi place open iid mailing list: fuyongjie@163.com and hh@qq.com; ..;, fuyongjie.100@yahoo.com, fu-1999@sina.com ";
$ EmailArr = getEmail ($ emailstr );
Echo"
";
print_r($emailArr);
echo "
";
?> Print as follows:
Array
(
9999@qq.com.cn
Fuyongjie@163.com
Hh@qq.com
Fuyongjie.100@yahoo.com
[4] => fu-1999@sina.com
) 3. comparison: the regular expressions in 2nd do not contain the ^ and $ Values of 1st;

View instances again

The code is as follows:

Function funcemail ($ str) // email regular expression
{
Return (preg_match ('/^ [_. 0-9a-z-a-z-] + @ ([0-9a-z] [0-9a-z-] + .) + [a-z] {2, 4} $/', $ str ))? True: false;
} // Verification method 1

$ Str = "qbcd@126.com.cn ";
Preg_match ("/^ [0-9a-z] + @ ([0-9a-z] +) [.]) + [a-z] {2, 3} $/", $ str, $ re );
Print_r ($ re); // email verification 2

If (eregi ("^ [_. 0-9a-z-] + @ ([0-9a-z] [0-9a-z-] + .) + [a-z] {2, 3} $ ", $ email )){
Echo "your email has passed the preliminary check ";
} // Third mailbox verification method

If (ereg ("/^ [a-z] ([a-z0-9] * [-_.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [.] [a-z] {2, 3} ([.] [a-z] {2 })? $/I; ", $ email )){
Echo "your email address is correct! ";}
Else {
Echo "please try again! ";
}

...

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.