PHP Check Email email function

Source: Internet
Author: User
Tags php form

A previously written php form e-mail sender, which verifies that the e-mail address format is correct by using the following method:

The code is as follows

Eregi ("^[_a-z0-9-]+" (. [ _a-z0-9-]+) *@[a-z0-9_-]+. [a-z0-9_-]+.* ", $email)

It was later discovered that an email address similar to the will. mistakenly written as, can also pass validation, such as user@126,com. After checking, it turns out that it actually only validates the user name section, so you find a tutorial on the web, which gives the following examples:

The code is as follows

Eregi (' ^[_a-z0-9-]+. [ _a-z0-9-]+) *@[a-z0-9-]+ (. [ a-z0-9-]+) *$ ', $email)

After checking, the email address user@126,com is still able to verify through it. An example was found:

The code is as follows

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

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

The code is as follows

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

Although it's easier to check the user name, it seems to work better now.

Example 1

The code is as follows

<?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 (written by oneself)

  code is as follows &nbs P;
Function is_valid_email_address ($email) { 
$qtext = ' [^//x 0d//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" &NBSP;
$quoted _string = "//x22 ($qtext | $quoted _pair) *//x22 ";  
$domain _ref = $atom;  
$sub _domain =" ($domain _ref| $domain _literal) " ;  
$word  = "($atom | $quoted _string)" &NBSP;
$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;&NBSP
}

/td>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

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.