Home  >  Article  >  Backend Development  >  How to implement the method code to determine whether the email is valid in PHP

How to implement the method code to determine whether the email is valid in PHP

不言
不言Original
2018-08-23 10:31:412912browse

The content of this article is about how PHP implements the method code to determine whether the email is valid. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

function validate_email($email){

    $exp="^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

    if(eregi($exp,$email)){ //先用正则表达式验证email格式的有效性

        if(checkdnsrr(array_pop(explode("@",$email)),"MX")){//再用checkdnsrr验证email的域名部分的有效性

            return true;

        }else{

            return false;

        }

    }else{

        return false;

    }

}

Note: The checkdnsrr function is invalid on the win host! The following is a solution proposed by a foreign programmer. He also wrote a function to replace the checkdnsrr function:

function myCheckDNSRR($hostName, $recType=''){

    if(!empty($hostName)){

        if( $recType=='' ) $recType="MX";

            exec("nslookup -type=$recType $hostName", $result);

        foreach($result as $line){

            if(eregi("^$hostName",$line)){

                return true;

            }

        }

       return false;

    }

    return false;

}

Related recommendations:

PHP email verification example tutorial, PHP email example

PHP comes with its own method to verify whether the email address exists, PHP comes with its own verification email address

The above is the detailed content of How to implement the method code to determine whether the email is valid in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn