PHP电子邮件地址验证问题
我想知道在输入框中输入电子邮件地址时如何检查是否包含 @
符号?我正在使用 PHP。
这是我的 php 代码。
if (isset($_POST['email']) && strlen($_POST['email']) <= 255)
I was wondering how can I just check if an @
sign has been included when an email address is entered into the input box? I'm using PHP.
Here is my php code.
if (isset($_POST['email']) && strlen($_POST['email']) <= 255)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这就是我用的。工作完美。
This is what I use. Works perfectly.
可以只使用一个简单的:
如果您绝对必须使用正则表达式,那么我会推荐(这表示正则表达式不应该用于验证电子邮件地址):
Could just use a simple:
If you absolutely must use a Regex, than I would recommend (This is to signify that regex should NOT be used to validate email addresses):
确定电子邮件是否正常的更好方法是
在 preg_match() 中使用此正则表达式
A better way to find if the email is fine is by using this regex
in preg_match()
您可能希望使用 preg 而不是松散的
strpos($_POST['email'],"@")
进行检查:来源
You might want to have this checked using a preg instead of a loose
strpos($_POST['email'],"@")
:Source
只是引用 strstr 上的文档。 “如果您只想确定某个特定的针是否出现在 haystack 中,请使用速度更快、内存占用更少的函数 strpos()。”
关于 strpos 我注意到的另一件事是,仅检查 strpos 的真实性是不安全的,因为如果 $needle 位于 $haystack 中的第 0 个位置,则简单的检查将失败。您还必须检查它的类型(至少我这样做)。这将打印“未找到”。
Just a quote from the docs on strstr. "If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead."
Another thing on strpos I've noticed is that it's unsafe to just check for truth of strpos because if $needle is at 0'th position in $haystack, a simple check will fail. You must check it's type as well (at least I do). This will print "notfound found".
此正则表达式实现 rfc2822:
然而,在实践中,我发现这更适合通过网页捕获电子邮件地址(或更具体地说是 ADDR-SPEC):
This regex implements rfc2822:
However, in practice, I find this more apposite for the purpose of capturing an email address (or more specifically an ADDR-SPEC) via a web page:
欢迎您使用我的免费 PHP 函数
is_email()
来验证地址。可以在此处下载。它不使用正则表达式,因为我找不到完全实现 RFC 5321 的正则表达式。is_email()
将确保地址完全符合 RFC 5321。它还可以选择检查域是否实际存在并具有 MX 记录。您不应该依赖验证器来告诉您用户的电子邮件地址是否确实存在:一些 ISP 向其用户提供不合规的地址,特别是在不使用拉丁字母的国家/地区。更多关于电子邮件验证的文章请参见:http://isemail.info/about。
You are welcome to use my free PHP function
is_email()
to validate addresses. It's available to download here. It doesn't use a regex because I can't find one that fully implements RFC 5321.is_email()
will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists and has an MX record.You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: http://isemail.info/about.
这是使用 PHP 进行电子邮件验证检查的方法:
This is how you do an email validation check with PHP: