简单
$email = '[email protected] ';
$validation = filter_var($email, FILTER_VALIDATE_EMAIL);
if ( $validation ) $output = 'proper email address';
else $output = 'wrong email address';
echo $output;
高级
此功能不仅会检查给定电子邮件地址的格式是否正确,还会测试主机是否存在。
<?php
$email="[email protected] ";
if (isValidEmail($email))
{
echo "Hooray! Adress is correct.";
}
else
{
echo "Sorry! No way.";
}
//Check-Function
function isValidEmail($email)
{
//Perform a basic syntax-Check
//If this check fails, there's no need to continue
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
return false;
}
//extract host
list($user, $host) = explode("@", $email);
//check, if host is accessible
if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
{
return false;
}
return true;
}
?>
正则表达式测试
function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$email))
{
return true;
}
return false;
}
以上,带有域名验证:
function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$email))
{
list($username,$domain)=split('@',$email);
if(!checkdnsrr($domain,'MX')) {
return false;
}
return true;
}
return false;
}
翻译自: https://css-tricks.com/snippets/php/email-address-validation/