Home > Backend Development > PHP Tutorial > Use PHP to verify whether the email is correct_PHP tutorial

Use PHP to verify whether the email is correct_PHP tutorial

WBOY
Release: 2016-07-13 10:59:10
Original
742 people have browsed it

When you register on a forum, there is usually an e-mail address verification function. When you enter an illegal format, an error message will appear.

​We can use the following regular expression
ereg("^[a-zA-Z0-9_] @[a-zA-Z0-9-] .[a-zA-Z0-9-.] $]", $email);

But the function of the above formula is that it can only check the string and cannot output it. We can further use this formula to achieve the function of returning information:
if (eregi("^[a-zA-Z0-9_] @[a-zA-Z0-9-] .[a-zA-Z0-9-.] $]", $email))
{
return FALSE;
}

Next we can further check whether the host name exists:
list($Username, $Domain) = split("@",$email);
if(getmxrr($Domain, $MXHost))
{
return TRUE;
}
else
{
if(fsockopen($Domain, 25, $errno, $errstr, 30))
{
return TRUE;
}
else
{
return FALSE;
}
}

Now we use PHP to organize the above two functions into a function:
function checkEmail($email)

{ if(eregi("^[a-zA-Z0-9_] @[a-zA-Z0-9-] .[a-zA-Z0-9-.] $]", $email))
{
return FALSE;
}

list($Username, $Domain) = split("@",$email);

if(getmxrr($Domain, $MXHost))
{
return TRUE;
}
else
{
if(fsockopen($Domain, 25, $errno, $errstr, 30))
{
return TRUE;
}
else
{
return FALSE;
}
}
}

After that, we can use this function to detect whether there is an entered Email, for example:

if(checkEmail(web@etoow.com) == FALSE)
{
echo "The E_mail you entered is incorrect.";
}
else
{
echo "The entered E_mail is correct.";
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631921.htmlTechArticleWhen you register on a forum, there is usually an e-mail address verification function. When an illegal format is entered, an error message will appear. We can use...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template