Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP的filter\u var filter\u VALIDATE\u电子邮件真的有效吗?_Php_Email Validation - Fatal编程技术网

PHP的filter\u var filter\u VALIDATE\u电子邮件真的有效吗?

PHP的filter\u var filter\u VALIDATE\u电子邮件真的有效吗?,php,email-validation,Php,Email Validation,在阅读了各种帖子之后,我决定不使用正则表达式来检查电子邮件是否有效,而只使用PHP内置的filter_var函数。它似乎工作正常,直到它开始告诉我一封电子邮件是无效的,因为我有一个号码在里面 即name@domain.com工作,而name2@domain.com没有 是我遗漏了什么,还是过滤var$电子邮件、过滤验证电子邮件真的很无效?name2@domain.com似乎效果不错: 但我确实看到人们抱怨它有问题,即使如此。很可能,它确实有问题,但正则表达式解决方案也会有问题。电子邮件地址规范

在阅读了各种帖子之后,我决定不使用正则表达式来检查电子邮件是否有效,而只使用PHP内置的filter_var函数。它似乎工作正常,直到它开始告诉我一封电子邮件是无效的,因为我有一个号码在里面

即name@domain.com工作,而name2@domain.com没有


是我遗漏了什么,还是过滤var$电子邮件、过滤验证电子邮件真的很无效?

name2@domain.com似乎效果不错:

但我确实看到人们抱怨它有问题,即使如此。很可能,它确实有问题,但正则表达式解决方案也会有问题。电子邮件地址规范非常非常复杂


这就是为什么验证电子邮件的唯一方法是向该地址发送电子邮件,并要求采取措施,例如:如果是注册脚本,请他们单击验证链接。

该过滤器最近已改进。 -在代码板使用的版本中,您的案例已正确过滤


你也可以看看和。Regexp非常可怕。

PHP5.3.3过滤代码中使用的正则表达式基于MichaelRushton的博客about。这似乎对你提到的案子有效

您还可以查看PHP中当前使用的regexp中的一些选项,这是经过测试的选项之一

然后您可以选择一个您更喜欢的regexp,并在调用preg_match时使用它


或者您可以使用regexp并替换文件PHP/ext/filter/logical\u filter.c中的一个,使用函数PHP\u filter\u validate\u email,然后重建PHP。

谢谢。我将发送一封验证电子邮件。如果有什么东西可以确保人们不会意外地输入明显错误的字符,那就太好了。@Keen我不是说将正则表达式验证抛出窗外;你可以验证它,如果它失败,警告用户。例如:计算机说你的电子邮件无效,但它不是很聪明。您确定要使用此电子邮件吗?测试电子邮件地址中是否存在点也是必要的@威尔·丹斯芬:你的问题不准确。name2@domain.com在所有支持filter_var的PHP版本上都可以很好地工作。证明:Michael Rushton博客的链接现在为您带来了一个空页面:@Matteo,您可以在PHP源代码中找到相同的代码。或者在wayback机器中查找Michael Rushton的博客:
function isValidEmail($email, $checkDNS = false)
{

    $valid = (
            /* Preference for native version of function */
            function_exists('filter_var') and filter_var($email, FILTER_VALIDATE_EMAIL)
            ) || (
                /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */
                strlen($email) <= 320
                /*
                 * The regex below is based on a regex by Michael Rushton.
                 * However, it is not identical. I changed it to only consider routeable
                 * addresses as valid. Michael's regex considers a@b a valid address
                 * which conflicts with section 2.3.5 of RFC 5321 which states that:
                 *
                 * Only resolvable, fully-qualified domain names (FQDNs) are permitted
                 * when domain names are used in SMTP. In other words, names that can
                 * be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
                 * in Section 5) are permitted, as are CNAME RRs whose targets can be
                 * resolved, in turn, to MX or address RRs. Local nicknames or
                 * unqualified names MUST NOT be used.
                 *
                 * This regex does not handle comments and folding whitespace. While
                 * this is technically valid in an email address, these parts aren't
                 * actually part of the address itself.
                 */
                and preg_match_all(
                    '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?))'.
                    '{255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?))'.
                    '{65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|'.
                    '(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))'.
                    '(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|'.
                    '(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\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',
                    $email)
            );

    if( $valid )
    {
        if( $checkDNS && ($domain = end(explode('@',$email, 2))) )
        {
            /*
            Note:
            Adding the dot enforces the root.
            The dot is sometimes necessary if you are searching for a fully qualified domain
            which has the same name as a host on your local domain.
            Of course the dot does not alter results that were OK anyway.
            */
            return checkdnsrr($domain . '.', 'MX');
        }
        return true;
    }
    return false;
}


//-----------------------------------------------------------------

    var_dump(isValidEmail('nechtan@tagon8inc.com', true));
    // bool(true)