Home > Article > Backend Development > How to implement the method code to determine whether the email is valid in PHP
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
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!