php 使用 preg_match 的电子邮件地址无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6453495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:26:00  来源:igfitidea点击:

Invalid email address using preg_match

php

提问by sark9012

Possible Duplicate:
How to validate an email in php5?

可能重复:
如何在 php5 中验证电子邮件?

I have used the following code to ensure email addresses provided on signup are valid.

我使用以下代码来确保注册时提供的电子邮件地址有效。

(!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email))

I entered a standard email address such as

我输入了一个标准的电子邮件地址,例如

[email protected]

[email protected]

and it is flagging up as being invalid.

它被标记为无效。

Can anyone help me with why this may be happening?

谁能帮助我解释为什么会发生这种情况?

回答by Shakti Singh

Why not just use filter_var

为什么不直接使用filter_var

var_dump(filter_var($email,FILTER_VALIDATE_EMAIL));

EDIT

编辑

if(filter_var($email,FILTER_VALIDATE_EMAIL) === false)
{
   echo 'Email is not valid';
}
else
{
   //do the stuff
}

回答by paxdiablo

Now can I ask youa question? What are those spaces doing in your regular expression? :-)

现在我可以问一个问题吗?这些空格在您的正则表达式中做什么?:-)

I'm pretty certain that spaces aren't actually valid in email addresses. And, even if they were, they wouldn't be required to be at specific positions relative to the separators (such as immediately before and after the @character).

我很确定空格在电子邮件地址中实际上无效。而且,即使是这样,它们也不需要位于相对于分隔符的特定位置(例如紧接在@字符之前和之后)。

Although I generally disagree with the use of regular expressions for email addresses (just send an email with a confirmation link - that solves your problem and then some a), you should at least use the right regular expression if you mustdo it that way.

虽然我通常不同意对电子邮件地址使用正则表达式(只需发送一封带有确认链接的电子邮件 - 可以解决您的问题,然后是一些a),但如果您必须这样做,您至少应该使用正确的正则表达式。



aThere are an untold number of perfectly valid email addresses that don't have an actual account behind them.

a有无数完全有效的电子邮件地址背后没有实际帐户。

回答by Jason Rogers

if you would like to use regex for matching emails, the following will match "sensible" addresses.

如果您想使用正则表达式匹配电子邮件,以下将匹配“合理”地址。

preg_match('/^([a-z0-9]+([_\.\-]{1}[a-z0-9]+)*){1}([@]){1}([a-z0-9]+([_\-]{1}[a-z0-9]+)*)+(([\.]{1}[a-z]{2,6}){0,3}){1}$/i', $email)

It's quite verbose but if you only want, like i said "sensible" addresses to pass - it does the job.

它非常冗长,但如果你只想要,就像我说的“合理”地址传递 - 它可以完成这项工作。

does get stuck on address like "example@somename.somewhere.com" because, after the @ symbol it looks for anything following a period to only be only 2-6 characters in length.

确实会卡在像“example@ somename.somewhere.com”这样的地址上,因为在@ 符号之后,它会查找句点之后的任何内容,长度只有 2-6 个字符。

"example@somename-somewhere.com" however would pass fine.

“example@ somename-somewhere.com”但是会通过。

I don't recommend trying to use a single regex solution for the job unless, as in my case, you only want to allow "sensible" addresses.

我不建议尝试使用单个正则表达式解决方案来完成这项工作,除非在我的情况下,您只想允许“合理”地址。

There is quite a good article that covers "correctly" validating email addresses here: http://www.linuxjournal.com/article/9585

这里有一篇很好的文章,涵盖了“正确”验证电子邮件地址:http: //www.linuxjournal.com/article/9585

回答by Marc Towler

With the new domainless addresses that are planned to be released, paxdiablo's solution seems even better

随着计划发布的新无域地址,paxdiablo的解决方案似乎更好