Php check email function (wonderful writing)

Source: Internet
Author: User
Tags php form

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: Copy code

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: Copy code

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: Copy code

Eregi ("^ [_. 0-9a-z-] + @ ([0-9a-z] [0-9a-z-] +.) + [a-z] {2, 3} $", $ str)

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: Copy code

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: Copy code

<? Php
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: Copy code

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;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.