PHP Check email e-mail function (wonderful writing) _php tutorial

Source: Internet
Author: User
Tags php form
A previously written php form e-mail sender, which uses the following method to verify that the e-mail address format is correct:

Code to copy code as follows
Eregi ("^[_a-z0-9-]+ (. [ _a-z0-9-]+) *@[a-z0-9_-]+. [a-z0-9_-]+.* ", $email)

It was later found that the e-mail address, similar to the. Mis-write, can also be verified, such as user@126,com. After checking that it actually validates only the user name part, it also finds a tutorial on the web, which gives the following examples:

Code to copy code as follows
Eregi (' ^[_a-z0-9-]+ (. [ _a-z0-9-]+) *@[a-z0-9-]+ (. [ a-z0-9-]+) *$ ', $email)

After checking, it is found that e-mail address user@126,com can still be verified by it. An example was found:

Code to copy code as follows
Eregi ("^[_.0-9a-z-]+@" ([0-9a-z][0-9a-z-]+.) +[a-z]{2,3}$ ", $str) www.111cn.net

This looks more reasonable because it validates the suffix name, although there are now 4 or more characters in the top-level domain, but only a minor modification is required. However, the e-mail address user@xxx,com 111cn.net still be able to pass the verification, after careful examination found that because there is no. escaped caused. Then make a slight change to it:

Code to copy code as follows
Eregi ("^[_.0-9a-z-]+@" ([0-9a-z][0-9a-z-]+.) +[a-z]{2,4}$)

Although it is a bit more relaxed about user name checking, it seems to be a good idea to use it now.


Example 1

Code to copy 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}) $ ", $email))
if ($test _mx)
{
List ($username, $domain) = Split ("@", $email);
Return Getmxrr ($domain, $mxrecords);
}
Else
return true;
Else
return false;
}
?>

Example 2 (self-written)

Code to copy code as follows
function Is_valid_email_address ($email) {
$qtext = ' [^//x0d//x22//x5c//x80-//xff] ';
$dtext = ' [^//x0d//x5b-//x5d//x80-//xff] ';
$atom = ' [^//x00-//x20//x22//x28//x29//x2c//x2e//x3a-//x3c '.
'//x3e//x40//x5b-//x5d//x7f-//xff]+ ';
$quoted _pair = '//x5c[//x00-//x7f] ';
$domain _literal = "//x5b ($dtext | $quoted _pair) *//x5d";
$quoted _string = "//x22 ($qtext | $quoted _pair) *//x22";
$domain _ref = $atom;
$sub _domain = "($domain _ref| $domain _literal)";
$word = "($atom | $quoted _string)";
$domain = "$sub _domain (//x2e$sub_domain) *";
$local _part = "$word (//x2e$word) *";
$addr _spec = "$local _part//x40$domain";
Return Preg_match ("!^ $addr _spec$!", $email)? 1:0;
}

For more detailed information, please see: http://www.111cn.net/phper/php-cy/57193.htm

http://www.bkjia.com/PHPjc/729848.html www.bkjia.com true http://www.bkjia.com/PHPjc/729848.html techarticle previously written a php form e-mail sender, which uses the following method to verify that the e-mail address format is correct: code as follows copy code eregi ("^[_a-z0-9-]+ (. [ _a-z0 ...

  • 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.