Php check email function (wonderful writing)

Source: Internet
Author: User
Tags php form
A previously written PHP form email sending program that uses the following method to verify that the email address format is correct: the code below copies the code eregi (^ [_ a-z0-9-] + (. [_ a-z0-9-] +) * @ [a-z0-9 _-] +. [a-z0-9 _-] +. *, $ email) later I found that. if it is written by mistake, the email address can also pass verification, for example, user @ 126

A previously written PHP form email sending program, which uses the following method to verify whether the email address format is correct: the code is as follows eregi ("^ [_ a-z0-9-] + (. [_ a-z0-9-] +) * @ [a-z0-9 _-] +. [a-z0-9 _-] +. * ", $ email) later, it was found that. if it is written by mistake, the email address can also pass verification, for example, user @ 126

A previously written PHP form email sending program, which uses the following method to verify that the email address format is correct:

The Code is as follows:
Eregi ("^ [_ a-z0-9-] + (. [_ a-z0-9-] +) * @ [a-z0-9 _-] +. [a-z0-9 _-] +. * ", $ email)

Later, it was found that the email address similar to writing. By mistake can also pass verification, such as user @ 126 and com. After checking, it is found that it only verifies the user name, so I found a tutorial on the Internet. The example is as follows:

The Code is as follows:
Eregi ('^ [_ a-z0-9-] + (. [_ a-z0-9-] +) * @ [a-z0-9-] + (. [a-z0-9-] +) * $ ', $ email)

After checking, it is found that the email address user @ 126 is still verified by com. An example is found:

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

This seems more reasonable because it verifies the suffix name. Although there are more than four top-level domain names, you only need to modify them a little. However, the email address user @ xxx and com 111cn.net can still pass verification. After careful check, it is found that the. Is not escaped. Then, make a slight modification to it:

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

Although it is easier to check the user name, it seems to have a good effect.


Example 1

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}) $ ", $ email ))
If ($ test_mx)
{
List ($ username, $ domain) = split ("@", $ email );
Return getmxrr ($ domain, $ mxrecords );
}
Else
Return true;
Else
Return false;
}
?>

Example 2 (self-written)

The Code is 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 details, see: http://www.111cn.net/phper/php-cy/57193.htm

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.