Home  >  Article  >  Backend Development  >  PHP check email email function (strange writing method)_PHP tutorial

PHP check email email function (strange writing method)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:38926browse

A PHP form email sending program I wrote before, in which the following method is used to verify whether the email address format is correct:

The code is as follows Copy the code
eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9_-]+.[a- z0-9_-]+.*", $email)

It was later discovered that email addresses similar to . mistakenly written as , could also pass verification, such as user@126,com. After checking, I found that it actually only verified the username part, so I found a tutorial online with the following example:

The code is as follows Copy the code
eregi('^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a -z0-9-]+)*$',$email)

After checking, it was found that the email address user@126,com was still able to pass its verification. Found an example:

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

This one looks more reasonable because it validates the suffix name. Although there are now top-level domain names with more than 4 characters, it only needs to be slightly modified. However, the email address user@xxx,com 111cn.net can still pass the verification. After careful inspection, it was found that it was because the . was not escaped. So I modified it slightly:

The code is as follows Copy the code
eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{ 2,4}$)

It seems to be working well for now, although it is a bit more lenient in checking usernames.


Example 1

Copy the 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 (written by myself)

Copy the 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 details, please check: http://www.111cn.net/phper/php-cy/57193.htm

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/729848.htmlTechArticleA PHP form email sending program I wrote before, in which the following method is used to verify whether the email address format is correct: The code is as follows. Copy the code eregi("^[_a-z0-9-]+(.[_a-z0...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn