Home  >  Article  >  Backend Development  >  Implementation and detailed explanation of regular expression of email address in PHP

Implementation and detailed explanation of regular expression of email address in PHP

高洛峰
高洛峰Original
2016-12-01 09:40:231270browse

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

In this regular expression, "+" means that the previous string appears one or more consecutively; "^" means that the next string must appear at the beginning, and "$" means the previous string Must appear at the end;
"." is also ".", where "" is the escape character; "{2,3}" means that the previous string can appear 2-3 times in a row. "()" means that the contained content must also appear in the target object. "[_.0-9a-z-]" means any character contained in "_", ".", "-", letters in the range from a to z, and numbers in the range from 0 to 9;
In this way, this regular expression can be translated like this:
"The following characters must be at the beginning (^)", "The characters must be contained in "_", ".", "-", from a to z Letters, numbers in the range from 0 to 9 ([_.0-9a-z-])", "The preceding character appears at least once (+)", @, "The string consists of a string from a to Starting with a letter in the range z, a number in the range 0 to 9, followed by at least one character contained in "-", any letter in the range a to z, any number in the range 0 to 9 characters, ending with . (([0-9a-z][0-9a-z-]+.))", "The previous character appears at least once (+)", "from a to z The letter appears 2-3 times and ends with it ([a-z]{2,3}$)"
Copy code 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;
}

The domain name consists of the specific character set of each country's language, English letters, numbers and "-" (i.e. hyphen or minus ), but cannot contain "-" at the beginning or end, and "-" cannot appear consecutively. The letters in the domain name are not case-sensitive. The domain name can be up to 60 bytes (including the suffix .com, .net). , .org, etc.).
/^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_] ?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?$/i;
/content/i constitutes a case-insensitive Regular expression;
^ Start of match

$ End of match

[a-z] E-Mail prefix must start with an English letter

([a-z0-9]*[-_]?[a-z0-9 ]+)* matches _a_2, aaa11, _1_a_2, but does not match a1_, aaff_33a_, a__aa. If it is a null character, it will also match. * means 0 or more.

* represents 0 or more previous characters.

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

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

[a-z0-9]+ matches one or more English letters or numbers, because "-" cannot be used as the end

@ There must be @

([a-z0 -9]*[-_]?[a-z0-9]+)+ See the explanation above ([a-z0-9]*[-_]?[a-z0-9]+)*, but it cannot be Empty, + means one or multiple.

[.] treats special characters (.) as ordinary characters

[a-z]{2,3} matches 2 to 3 English letters, usually com or net, etc.

([.][a-z]{ 2})? Matches 0 or 1 [.][a-z]{2} (such as .cn, etc.) I don’t know if the last part of .com.cn is usually two digits. If not, please modify {2 } is {number of starting words, number of ending words}

Perfect E-Mail regular expression, with detailed explanation, please help test it! 2. Extract email from the string:
Copy code 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俺not米vi Open the iid mailing list locally: fuyongjie@163.com and hh@qq.com;, fuyongjie.100@yahoo.com, fu-1999@sina.com";
$emailArr = getEmail($emailstr);
echo "

"; <br>print_r($emailArr); <br>echo "
";
?>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: The ^ and $ in the 1st are not included in the regular expression in the 2nd one;

Look at the example again
Copy the code The code is 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;
}//Verification method one
$str="qbcd@126.com.cn";
preg_match("/^[0-9a-z]+@(([0-9a-z]+)[.])+[a-z]{2,3}$/",$str,$re);
print_r($re);//Email verification two
if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+ [a-z]{2,3}$",$email)) {
echo "Your email has passed the preliminary check";
}//The third email 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!";
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn