php email检测类(附示例)

发布时间:2019-11-16编辑:脚本学堂
分享一个php实现的email检测类,同样是用到了php正则,简单实用,有需要的朋友参考下。

php实现的email检测类,判断email格式的正确性。

代码:

<? 
//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>"; 
        } 
    } 
} 
?>

调用示例:

<? 
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); 
?>