【问题标题】:Can the domain part of an email address be an IP addresses?电子邮件地址的域部分可以是 IP 地址吗?
【发布时间】:2020-06-20 05:39:02
【问题描述】:

我正在尝试构建一个正则表达式来检测有效的电子邮件。我在 Stack Overflow 上浏览了一些文章。我还阅读了section 3.4.1 of RFC 2822,它描述了电子邮件地址的不同组成部分:

addr-spec       =       local-part "@" domain

local-part      =       dot-atom / quoted-string / obs-local-part

domain          =       dot-atom / domain-literal / obs-domain

domain-literal  =       [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS]

dcontent        =       dtext / quoted-pair

dtext           =       NO-WS-CTL /     ; Non white space controls

                        %d33-90 /       ; The rest of the US-ASCII
                        %d94-126        ;  characters not including "[",
                                        ;  "]", or "\"

用简单的英语,有人能解释一下上面的定义吗?是否允许使用info@[192.168.0.1] 之类的电子邮件?

我知道我不必重新发明轮子,而且我可以使用一些很好的库来验证电子邮件。我只是想了解事情在幕后是如何运作的。

【问题讨论】:

标签: regex ip-address email-validation


【解决方案1】:

在 PHP 中,您可以使用 FILTER_VALIDATE_EMAIL。不需要库

<?php
$email = "john.doe@example.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo("$email is a valid email address");
} else {
  echo("$email is not a valid email address");
}
?>

【讨论】:

    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 2011-01-12
    • 2022-07-14
    • 2014-09-18
    • 2017-07-23
    • 2020-05-27
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多