本文介绍了PHP的filter_var FILTER_VALIDATE_EMAIL是否真正起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读各种文章后,我决定不使用REGEX来检查电子邮件是否有效,而仅使用PHP的内置filter_var函数.似乎正常,直到它开始告诉我一封电子邮件是无效的,因为我有一个号码.

After reading various posts I decided not to use REGEX to check if an email is valid and simply use PHP's inbuilt filter_var function. It seemed to work ok, until it started telling me an email was invalid because I had a number in it.

即name@domain.com有效,而name2@domain.com无效.

ie name@domain.com works, while name2@domain.com doesn't.

我错过了什么还是filter_var($email, FILTER_VALIDATE_EMAIL)真的无效吗?

Am I missing something or is the filter_var($email, FILTER_VALIDATE_EMAIL) really quite ineffective?

推荐答案

PHP 5.3.3过滤器代码中使用的正则表达式基于Michael Rushton关于电子邮件地址验证.似乎确实适合您提到的情况.

The regular expression used in the PHP 5.3.3 filter code is based on Michael Rushton's blog about Email Address Validation. It does seem to work for the case you mention.

您还可以在比较电子邮件地址验证中查看一些选项.正则表达式(PHP当前使用的regexp是经过测试的正则表达式之一).

You could also check out some of the options in Comparing E-mail Address Validating Regular Expressions (the regexp currently used in PHP is one of those tested).

然后,您可以选择一个更喜欢的正则表达式,并将其用于对preg_match()的调用.

Then you could choose a regexp you like better, and use it in a call to preg_match().

否则,您可以使用正则表达式并替换PHP/ext/filter/logical_filter.c文件中的正则表达式,函数php_filter_validate_email()并重建PHP.

Or else you could take the regexp and replace the one in file PHP/ext/filter/logical_filter.c, function php_filter_validate_email(), and rebuild PHP.

这篇关于PHP的filter_var FILTER_VALIDATE_EMAIL是否真正起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:50