教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php email检测类(附示例)

php email检测类(附示例)

发布时间:2016-11-30   编辑:jiaochengji.com
分享一个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); 
?>

您可能感兴趣的文章:
php email检测类(附示例)
php email加密类(附实例)
常用js验证代码大全(Email、手机号码、身份证号码、文件类型等)
php实例:email验证类
php正则验证email地址的例子
php邮箱检测的正则表达式一例
php验证Email地址的演示示例
检测上传文件类型与大小的js代码
用PHP来验证Email是否正确
PHPMailer发送带附件邮件的例子

[关闭]
~ ~