PHP Email address Regular expression implementation and detailed _php skills

Source: Internet
Author: User
Tags php email
First attach the Code
Copy Code code as follows:

^[_.0-9a-z-]+@ ([0-9a-z][0-9a-z-]+.) +[a-z]{2,3}$

In this regular expression, "+" means one or more occurrences of the preceding string; "^" means that the next string must appear at the beginning, and "$" indicates that the previous string must appear at the end;
“.” That is, ".", where "" is an escape character; "{2,3}" means that the preceding string can appear 2-3 consecutive times. "()" means that the contained content must appear in the target object at the same time. "[_.0-9a-z-]" means any character contained in "_", ".", "-", letters from A to Z range, numbers from 0 to 9;
Thus, this regular expression can be translated as follows:
"The following characters must be at the beginning (^)", "the character must be contained in" _ ",". ","-", letters from A to Z range, numbers from 0 to 9 ([_.0-9a-z-]), and" preceded by at least one (+) ", @," The string consists of a letter that is contained in the range from A to Z, starts with a character from a number in the range 0 through 9, followed by at least one of the characters contained in "-", any letter from A to Z range, from 0 to 9, and finally. End ([0-9a-z][0-9a-z- ]+.) "", "the preceding character appears at least once (+)", "the letter from A to Z appears 2-3 times, and ends with it ([a-z]{2,3}$)"
Copy Code code 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;
}

Domain names are composed of specific character sets, English letters, numbers, and "-" (that is, hyphens or minus signs) of each country's text, but neither the beginning nor the end can contain "-", "-" and "-" cannot appear continuously. The letters in the domain name are not case-insensitive. The domain name can be up to 60 bytes (including suffix. com,. NET,. org, and so on).
/^[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 start

$ match End

[A-z] e-mail prefix must be an English letter beginning

([A-z0-9]*[-_]? [a-z0-9]+) * and _a_2, AAA11, _1_a_2 match, and A1_, Aaff_33a_, A__aa mismatch, if it is a null character, is also a match, * represents 0 or more.

* Represents 0 or more preceding characters.

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

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

[a-z0-9]+ matches 1 or more English letters or numbers, because "-" can not be done as the end

@ must have a @

([A-z0-9]*[-_]? [a-z0-9]+] + see above ([a-z0-9]*[-_]?[ a-z0-9]+) * explanation, but cannot be null, + represents one or more.

[.] The special character (.) As a normal character

[A-z] {2,3} matches 2 to 3 English letters, typically COM or net.

([.] [A-z] {2})? Match 0 or 1 [.] [A-z] {2} (for example, CN, etc.) I don't know. com.cn the last part is not all two digits, if not please modify {2} to {starting words, end words}

Perfect e-mail regular expression, with detailed explanation, please help test! 2. Extract the email in the string:
Copy Code code as follows:

<?php
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 am not the M VI Place to open IID mailing list: fuyongjie@163.com and hh@qq.com;. ;;, fuyongjie.100@yahoo.com,fu-1999@sina.com ";
$EMAILARR = Getemail ($EMAILSTR);
echo "<pre>";
Print_r ($EMAILARR);
echo "</pre>";
?> print as follows:
Array
(
[0] =>9999@qq.com.cn
[1] =>fuyongjie@163.com
[2] =>hh@qq.com
[3] =>fuyongjie.100@yahoo.com
[4] =>fu-1999@sina.com
3. Comparison: In the 2nd, there are no 1th ^ and $;

Look at the example again
Copy Code code as follows:
function Funcemail ($STR)/mailbox Regular expression
{
return (Preg_match ('/^[_.0-9a -z-a-z-]+@ ([0-9a-z][0-9a-z-]+.) +[a-z]{2,4}$/', $str))? True:false;
}//authentication Method One
$str = "qbcd@126.com.cn";
Preg_match (/^[0-9a-z]+@ ([0-9a-z]+) [.]) +[a-z]{2,3}$/", $str, $re);
Print_r ($re);//mailbox Authentication two
if (eregi ("^[_.0-9a-z-]+@") ([0-9a-z][0-9a-z-]+.) +[a-z]{2,3}$ ", $email)) {
echo" your e-mail through preliminary check ";
}//The third mailbox authentication 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.