如何在 PHP 中验证电子邮件?
如何使用 php5 验证输入值是有效的电子邮件地址。现在我正在使用此代码
function isValidEmail($email){
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
}
,但它显示已弃用的错误。我该如何解决这个问题。请帮我。
How can I validate the input value is a valid email address using php5. Now I am using this code
function isValidEmail($email){
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
}
but it shows deprecated error. How can I fix this issue. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
filter_var()
函数,它为您提供了许多方便的验证和清理选项。PHP 手册 filter_var()
适用于 PHP >= 5.2 .0
如果您不想更改依赖于您的函数的代码,只需执行以下操作:
注意< /strong>:对于其他用途(需要正则表达式的地方),已弃用的
ereg
函数系列(POSIX Regex 函数)应替换为preg
系列(PCRE 正则表达式函数)。存在少量差异,阅读手册就足够了。更新1:正如所指出的@binaryLV:
此错误已被修复。
更新 2:此方法当然会将
bazmega@kapa
验证为有效的电子邮件地址,因为事实上它是一个有效的电子邮件地址。但大多数时候在互联网上,您还希望电子邮件地址具有 TLD:[电子邮件受保护]
。正如这篇博客文章中所建议的(链接由@Istiaque Ahmed),您可以增强filter_var ()
,使用正则表达式检查域部分中是否存在点(但不会检查有效 TLD):如 @Eliseo Ocampos 指出,这个问题只存在于 PHP 5.3 之前,其中版本中他们更改了正则表达式,现在它执行此检查,因此您不必这样做。
You can use the
filter_var()
function, which gives you a lot of handy validation and sanitization options.PHP Manual filter_var()
Available in PHP >= 5.2.0
If you don't want to change your code that relied on your function, just do:
Note: For other uses (where you need Regex), the deprecated
ereg
function family (POSIX Regex Functions) should be replaced by thepreg
family (PCRE Regex Functions). There are a small amount of differences, reading the Manual should suffice.Update 1: As pointed out by @binaryLV:
This bug has already been fixed.
Update 2: This method will of course validate
bazmega@kapa
as a valid email address, because in fact it is a valid email address. But most of the time on the Internet, you also want the email address to have a TLD:[email protected]
. As suggested in this blog post (link posted by @Istiaque Ahmed), you can augmentfilter_var()
with a regex that will check for the existence of a dot in the domain part (will not check for a valid TLD though):As @Eliseo Ocampos pointed out, this problem only exists before PHP 5.3, in that version they changed the regex and now it does this check, so you do not have to.
请参阅 http://www.php.net/manual/en/function 处的注释.ereg.php:
See the notes at http://www.php.net/manual/en/function.ereg.php:
这是旧帖子,但我将分享一个我的解决方案,因为之前没有人在这里提到一个问题。
新的电子邮件地址可以包含 UTF-8 字符或特殊域名,例如
.live
、.news
等。我还发现某些电子邮件地址可以使用西里尔字母并且在所有情况下标准正则表达式或
filter_var()
将失败。这就是为什么我为此制定了一个解决方案:
此功能适用于所有案例和电子邮件格式。
This is old post but I will share one my solution because noone mention here one problem before.
New email address can contain UTF-8 characters or special domain names like
.live
,.news
etc.Also I find that some email address can be on Cyrilic and on all cases standard regex or
filter_var()
will fail.That's why I made an solution for it:
This function work perfectly for all cases and email formats.
我总是用这个:
I always use this:
验证电子邮件之前:首先,您必须删除电子邮件中的所有非法字符。
之后使用此
filter_var()
函数验证您的电子邮件地址。例如
Before validation of Email: First you have to remove all illegal characters from email.
after that validate your email address using this
filter_var()
function.For e.g.
使用:
Use:
请注意,地址为[电子邮件受保护] 是无效的,但 filter_var() 返回 true,许多其他字符串(电子邮件)使用 filter_var() 无效返回 true。
为了验证电子邮件,我使用此功能:
请改进并分享,谢谢
take several care, a address as [email protected] is INVALID, but filter_var() return true, many others strings (emails) INVALIDS return true using filter_var().
for validate email I use this function:
please improve and share, thanks