发送正则表达式 [英] Email regular expression
问题描述
可能重复:
验证电子邮件地址的最佳正则表达式是什么?
Duplicate: What is the best regular expression for validating email addresses?
我知道这是一个常见的问题,但在验证电子邮件地址时,似乎仍然无法找到一个很好的正则表达式
I know this is a common question, but I still can't seem to find a great regular expression to use when validating email addresses.
我真的没有时间去阅读规范并写我自己的。你以前使用过什么,并且运作良好?我不太在乎100%的匹配规范,但越接近越好。
I don't really have time to go read the spec and write my own. What have ya'll used before, and has it worked well? I don't really care about 100% matching the spec, but the closer the better.
推荐答案
这是一个我使用的功能。它只是通过正则表达式运行电子邮件地址,但到目前为止,它是我找到的最完整的解决方案:
Here's a function that I use. It does a little more than just run the email address through a regex, but so far it is the most complete solution that I found:
function validEmail($email, $skipDNS = false)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if(!$skipDNS)
{
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
}
return $isValid;
}
该函数有一个可选的$ skipDNS参数,如果您不想验证hos的MX记录,可以设置为TRUE。否则,该功能将尝试验证所提供的电子邮件地址是否实际映射到真实的电子邮件服务器。
The function has an optional $skipDNS argument that can be set to TRUE if you don't want to validate the MX records for the hos. Otherwise the function will attempt to validate that the e-mail address provided actually maps to a real email server.
注意大多数RegEx电子邮件验证技术将验证最有用电子邮件地址,但他们很可能允许一些精心制作的无效地址或最坏的..失败在一些更模糊,但有效的电子邮件地址。有关更多信息,您可能需要查看 Internet邮件格式RFC ,其中描述了e的允许格式邮件地址。
It's useful to note that most RegEx email validation techniques will validate most e-mail addresses but they will most likely allow some carefully crafted invalid addresses or worst.. fail on some more obscure, but valid e-mail addresses. For more information you may want to check out the Internet Message Formats RFC which describes the allowed format for e-mail addresses.
这篇关于发送正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!