在 PHP 中使用正则表达式进行电子邮件验证 [英] Email validation using regular expression in PHP

查看:38
本文介绍了在 PHP 中使用正则表达式进行电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对正则表达式很陌生.我正在用 PHP 开发一个项目,我需要验证电子邮件地址.在本网站和谷歌搜索后,我发现以下正则表达式表示它应该最有效.

I am pretty much new with regular expression. I am developing a project in PHP and i need to validate email address. After searching in this site and google i found the following regular expression says it should work best.

if (preg_match("/^(?!(?:(?:x22?x5C[x00-x7E]x22?)|(?:x22?[^x5Cx22]x22?)){255,})(?!(?:(?:x22?x5C[x00-x7E]x22?)|(?:x22?[^x5Cx22]x22?)){65,}@)(?:(?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)|(?:x22(?:[x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-x7F]|(?:x5C[x00-x7F]))*x22))(?:.(?:(?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)|(?:x22(?:[x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-x7F]|(?:x5C[x00-x7F]))*x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$/iD", strtolower($mail_address))) {
    echo "valid email";
}

但是当我使用它时,我得到错误提示,

But when I use it I get error says,

"Warning: preg_match(): No ending delimiter '/' found"

这段代码有什么问题?

推荐答案

用这个代替正则表达式:

Use this instead of a regular expression:

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
     //Valid email!
}

不推荐使用正则表达式来验证电子邮件地址,因为它肯定是不漂亮,特别是如果您不想排除拥有正确的有效电子邮件地址的人到 RFC 822 语法.

Using regular expressions to validate email addresses is not recommended as it is certainly not pretty, especially if you don't want to exclude somebody who has a valid email address that is correct according to RFC 822 grammar.

http://www.php.net/filter_var 是您最好的选择.

尝试根据 RFC 822 语法验证电子邮件地址的正则表达式的疯狂示例:

A crazy example of a regex that attempts to validate email addresses according to RFC 822 grammar:

(?:(?:
)?[ 	])*(?:(?:(?:[^()<>@,;:\".[] 00-31]+(?:(?:(?:
)?[ 	]
)+||(?=[["()<>@,;:\".[]]))|"(?:[^"
\]|\.|(?:(?:
)?[ 	]))*"(?:(?:

)?[ 	])*)(?:.(?:(?:
)?[ 	])*(?:[^()<>@,;:\".[] 00-31]+(?:(?:(
?:
)?[ 	])+||(?=[["()<>@,;:\".[]]))|"(?:[^"
\]|\.|(?:(?:
)?[ 
	]))*"(?:(?:
)?[ 	])*))*@(?:(?:
)?[ 	])*(?:[^()<>@,;:\".[] 00-
31]+(?:(?:(?:
)?[ 	])+||(?=[["()<>@,;:\".[]]))|[([^[]
\]|\.)*
](?:(?:
)?[ 	])*)(?:.(?:(?:
)?[ 	])*(?:[^()<>@,;:\".[] 00-31]+
(?:(?:(?:
)?[ 	])+||(?=[["()<>@,;:\".[]]))|[([^[]
\]|\.)*](?:
(?:
)?[ 	])*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?:[^()<>@,;:\".[] 00-31]+(?:(?:(?:
)?[ 	])+|
|(?=[["()<>@,;:\".[]]))|[([^[]
\]|\.)*](?:(?:
)?[ 	])*))*>(?:(
?:
)?[ 	])*))*)?;s*)

这篇关于在 PHP 中使用正则表达式进行电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆