如何在 PHP 中验证电子邮件? [英] How to validate an Email in PHP?

查看:36
本文介绍了如何在 PHP 中验证电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 php5 验证输入值是否为有效的电子邮件地址.现在我正在使用此代码

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.

推荐答案

您可以使用 filter_var() 函数,该函数为您提供了许多方便的验证和清理选项.

You can use the filter_var() function, which gives you a lot of handy validation and sanitization options.

filter_var($email, FILTER_VALIDATE_EMAIL)

  • PHP 手册 filter_var()

    PHP >= 5.2.0

    如果您不想更改依赖于您的函数的代码,请执行以下操作:

    If you don't want to change your code that relied on your function, just do:

    function isValidEmail($email){ 
        return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
    }
    

    注意:对于其他用途(需要正则表达式),已弃用的 ereg 函数系列(POSIX 正则表达式函数)应替换为 preg 系列(PCRE 正则表达式函数).存在少量差异,阅读手册应该就可以了.

    Note: For other uses (where you need Regex), the deprecated ereg function family (POSIX Regex Functions) should be replaced by the preg family (PCRE Regex Functions). There are a small amount of differences, reading the Manual should suffice.

    更新 1:正如 @binaryLV:

    PHP 5.3.3 和 5.2.14 有一个与以下相关的错误FILTER_VALIDATE_EMAIL,验证时导致segfault大值.简单而安全的解决方法是使用 strlen()filter_var() 之前.我不确定 5.3.4 final,但它是写到一些 5.3.4-snapshot 版本也受到影响.

    PHP 5.3.3 and 5.2.14 had a bug related to FILTER_VALIDATE_EMAIL, which resulted in segfault when validating large values. Simple and safe workaround for this is using strlen() before filter_var(). I'm not sure about 5.3.4 final, but it is written that some 5.3.4-snapshot versions also were affected.

    此错误已修复.

    更新 2:这个方法当然会验证 bazmega@kapa 是一个有效的电子邮件地址,因为它实际上是一个有效的电子邮件地址.但大多数时候在 Internet 上,您还希望电子邮件地址具有 TLD:bazmega@kapa.com.正如这篇博客文章(链接由@Istiaque Ahmed),你可以增加 filter_var() 使用正则表达式检查域部分中是否存在点(但不会检查 valid TLD):

    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: bazmega@kapa.com. As suggested in this blog post (link posted by @Istiaque Ahmed), you can augment filter_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):

    function isValidEmail($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL) 
            && preg_match('/@.+./', $email);
    }
    

    正如 @Eliseo Ocampos 指出的那样,这个问题只存在于PHP 5.3之前,在那个版本中,他们更改了正则表达式,现在它会执行此检查,因此您不必这样做.

    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.

    这篇关于如何在 PHP 中验证电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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