The realization and explanation of mailbox address regular expression in php

  • 2020-05-16 06:32:32
  • OfStack

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

In this regular expression, "+" means that the preceding string appears one or more in succession. "^" means that the next string must appear at the beginning, and "$" means that the first string must appear at the end.
". "is". ", where "" is an escape; "{2,3}" means that the preceding string can appear 2-3 times in a row. "()" means that the included content must also appear in the target object. "[_. 0-9a-z -]" means any character contained in "_", ". ", "-", letters from a to z, and Numbers from 0 to 9;
In this way, the regular expression can be translated as:
"Must be in the beginning of the following characters (^)", "the characters must be included in the" _ ", ". ", "-", from a to z within the scope of the letters and the Numbers range from 0 to 9 ([_. 0-9 a z -]) ", "the character in front of the at least one (+)", @, "the string by 1 contained within the range from a to z a letter, from 0 to 9 within the scope of the number of characters in the beginning, (([0-9a-z][0-9a-z -]+.)) ", "this character appears at least once (+)", "letters from a to z appear 2-3 times and end with it ([a-z]{2,3}$)".
 
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 a combination of a specific character set, English letters, Numbers, and "-" (i.e. a hyphen or a minus sign), but does not begin or end with "-" and "-" does not appear continuously. Domain names are case-insensitive. Domain names can be up to 60 bytes long (including suffixes.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;
^ match start

End of $match

[a-z] the E-Mail prefix must begin with a letter

([a z0-9] * [_]? [a-z0-9]+)* matches _a_2, aaa11, _1_a_2, and a1_, aaff_33a_, a58en. If it is empty, it also matches, * means zero or more.

* represents zero or more preceding characters.

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

[_]? Matches 0 or 1 "-", because "-" does not appear continuously

[a-z0-9]+ matches one or more letters or Numbers, because "-" does not end

@ must have an @

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

[.] treat special characters (.) as normal characters

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

([.] [a - z] {2})? Match 0 or 1 [.][a-z]{2}(e.g.,.cn, etc.) I don't know if the last part of 1.com.cn is all two-digit, if not, please modify {2} to {start and end words}

Perfect E-Mail regular expression, attached detailed explanation, please help to test 1! 2. Extract email from the string:
 
<?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 The an is not m vi Place is 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: the first 2 In the regular there is no 1 the ^ and $; 

Then look at instance
 
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; 
}// Validation 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 address verification 2 
if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email)) { 
echo " your  e-mail  Pass the preliminary inspection "; 
}// The first 3 A 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!"; 
} 

Related articles: