Home  >  Article  >  Backend Development  >  php email detection class (with examples)

php email detection class (with examples)

WBOY
WBOYOriginal
2016-07-25 08:56:351094browse
Share an email detection class implemented in PHP. It also uses PHP regular rules. It is simple and practical. Friends in need can refer to it.

The email detection class implemented by PHP determines the correctness of the email format.

Code:

<? 
//email格式检测
class check_email{ 
    private $email; 
    private $exp="%^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$%"; 
    private $success_txt; 
    private $error_txt; 
    /* 
     * $email - 待检测email地址
     * $exp - 正则验证
     * $success_txt - email格式有效时的提示消息
     * $error_txt - email格式无效时的错误消息
     */     
     
    function result_txt($success_txt,$error_txt){ 
        $this->success_txt=$success_txt; 
        $this->error_txt=$error_txt; 
    } 
     
    function start_check($params){ 
        $this->email=$params; 
        if(preg_match($this->exp, $this->email)){ 
            return $this->echo_result($this->email,true); 
            /*输入的email格式有效*/ 
        }else{ 
            return $this->echo_result($this->email,false); 
            /*输入的email格式无效*/ 
        } 
    } 
     
    function echo_result($email,$result){ 
        if($result){ 
            return $email." [".$this->success_txt."]<br>"; 
        }else{ 
            return "<span style='text-decoration:line-through'>".$email."</span> [".$this->error_txt."]<br>"; 
        } 
    } 
} 
?>

Call example:

<? 
require_once("check.inc.php"); 
$email_1="test@test.te"; 
$email_2="test@testte"; 

$a=new check_email; 
/*Show text ->  [如果email格式有效]  [如果email格式无效]  */ 
$a->result_txt("email格式有效","email格式无效"); 
echo $a->start_check($email_1); 
echo $a->start_check($email_2); 
?>


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