PHP Check the mailbox address of a lot of more commonly used to write their own regular, but is a lot of trouble, I have the method of PHP with a check.
Filter_var
Filter_var is a built-in variable filtering method in PHP that provides a number of useful filters that can be used to validate integers, floating-point numbers, mailboxes, URLs, MAC addresses, and so on.
Specific filter Reference: Filters.validate
Filter_var returns FALSE, indicating that the variable cannot pass through the filter, which is illegal.
$email = "lastchiliarch@163.com";
Var_dump (Filter_var ($email, Filter_validate_email));
$email = "ASB";
Var_dump (Filter_var ($email, Filter_validate_email));
$email = "1@a.com";
Var_dump (Filter_var ($email, Filter_validate_email));
Output:
string "lastchiliarch@163.com"
bool (FALSE)
string (7) "1@a.com"
For ASB This illegal mailbox format returned false, but for 1@a.com passed, or slightly flawed ah.
But the general is also through will think 1@a.com is a legitimate mailbox, then what method can more accurate verification?
Checkdnsrr
CHECKDNSRR is actually used to query DNS records for the specified host, which we can use to verify that the mailbox exists.
The MX record is certainly not present for 1@a.com.
$email = "lastchiliarch@163.com";
Var_dump (CHECKDNSRR (Array_pop ("@", $email)), "MX");
$email = "1@a.com";
Var_dump (CHECKDNSRR (Array_pop ("@", $email)), "MX");
Output:
bool (TRUE)
bool (FALSE)
Can see, very perfect, the only drawback is too slow, after all, to do a network request. So it is not appropriate to sync to a large number of mailboxes using this approach to verify.
Filter_var+checkdnsrr
We can engage Filter_var and CHECKDNSRR to do the check, for the vast majority of illegal mailboxes will certainly be in Filter_var when the hang off, the rest of the reuse
CHECKDNSRR further judgment.
$email _arr = Array ("lastchiliarch@163.com", "1@a.com");
foreach ($email _arr as $email) {
if (Filter_var ($email) = False) {
echo "invalid email: $email \ n";
Continue;
}
if (CHECKDNSRR array_pop (Explode ("@", $email), "MX") = = False) {
echo "invalid email: $email \ n";
Continue
}
}
Output: Invalid email:1@a.com
Note, however, that because only the MX record is checked, only 163.com can be judged to exist, but it cannot be explained that lastchiliarch this user is present.
To more accurately determine the existence of a mailbox, it can only be connected to the SMTP server to authenticate.
The following for you to share the PHP e-mail address regular expression verification, the specific contents are as follows
<?php header ("content-type:text/html;
Charset=utf-8 ");
$reply = "";
if (Isset ($_post["email_address"])) {$email _address = $_post["email_address"]; $pattern = "/^" ([0-9a-za-z\\-_\\.] +) @ ([0-9a-z]+\\. [A-z] {2,3} (\\. [A-z] {2})?
$/i ";
if (Preg_match ($pattern, $email _address)) {$reply = "The e-mail address you entered is valid <br/><br/>\n";
$user _name = preg_replace ($pattern, "$", $email _address);
$domain _name = preg_replace ($pattern, "$", $email _address); $reply. = "User name:". $user _name. "
<br/>\n "; $reply. = "Domain name:". $domain _name. "
<br/>\n\n ";
else {$reply = "The e-mail address you entered is not legal"; }}?> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
I hope you enjoy this article and help us.