Home  >  Article  >  Backend Development  >  PHP regular expression verification email_PHP tutorial

PHP regular expression verification email_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:451158browse

Email verification codes are mostly unable to verify one-letter domain names or one-letter usernames, such as: i@fufuok.com or fufu@9.cn. The solution is as follows:

1. Do not judge the total length. You can add the length judgment yourself;

2, supports domain name suffixes such as .net.cn and .com.cn;

3. The email name part starts with a letter or number, and can have "-" and "_" symbols in the middle;

4. The domain name part starts with a letter or number, and there can be "-" and "_" symbols in the middle;


PHP email verification regular expression

Example
The code is as follows
 代码如下 复制代码

preg_match("/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i",$email );

Copy code

 代码如下 复制代码

/**
 * 自己修整的一个邮箱正则表达式
 * 琼台博客
 */
echo '';
function c_email($email){
    $reg='/^([a-zA-Z0-9]{1,20})(([_-.])?([a-zA-Z0-9]{1,20}))*@([a-zA-Z0-9]{1,20})(([-_])?([a-zA-Z0-9]{1,20}))*(.[a-z]{2,4}){1,2}$/';
    if(preg_match($reg,$email))
        return true;    
    return false;
}
$email = 'mail@lizhong.me';
$check_result = c_email($email);
if($check_result){
    echo '邮箱格式正确';
}else{
    echo '邮箱格式错误';
}

preg_match("/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i", $email );

 代码如下 复制代码

class Reg
{
public $mail;
function __construct()
{
$this->mail = $_POST["mail"];       
    }
    function RegMail()
    {
        if(preg_match("/^[0-9a-zA-Z]+(?:[_-][a-z0-9-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*.[a-zA-Z]+$/i", $this->mail))
        {
            echo "";
        }
        else
        {
            echo "";
        }
    }
}
$r = new Reg();
$r->RegMail();
$strings = "abc@163.com";
if(preg_match("/^[0-9a-zA-Z]+(?:[_-][a-z0-9-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*.[a-zA-Z]+$/i",$strings))
{
    echo"验证成功!是邮箱地址。";
}
else
{
    echo"不是邮箱地址!";
}
?>

The code is as follows Copy code
/** * An email regular expression modified by myself * Qiongtai Blog ​*/ echo ''; function c_email($email){ $reg='/^([a-zA-Z0-9]{1,20})(([_-.])?([a-zA-Z0-9]{1,20}))*@ ([a-zA-Z0-9]{1,20})(([-_])?([a-zA-Z0-9]{1,20}))*(.[a-z]{2, 4}){1,2}$/'; If(preg_match($reg,$email))          return true;                                           Return false; } $email = 'mail@lizhong.me'; $check_result = c_email($email); if($check_result){ echo 'The email format is correct'; }else{ echo 'Email format error'; } Email verification class
The code is as follows Copy code
class Reg<🎜> {<🎜> Public $mail;<🎜> Function __construct()<🎜> {<🎜>            $this->mail = $_POST["mail"];                                                                               } Function RegMail() { if(preg_match("/^[0-9a-zA-Z]+(?:[_-][a-z0-9-]+)*@[a-zA-Z0-9]+(?:[ -.][a-zA-Z0-9]+)*.[a-zA-Z]+$/i", $this->mail))             { echo ""; }         else             { echo ""; } } } $r = new Reg(); $r->RegMail(); $strings = "abc@163.com"; if(preg_match("/^[0-9a-zA-Z]+(?:[_-][a-z0-9-]+)*@[a-zA-Z0-9]+(?:[ -.][a-zA-Z0-9]+)*.[a-zA-Z]+$/i",$strings)) { echo "Verification successful! It's the email address."; } else { Echo "Not an email address!"; } ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631574.htmlTechArticleEmail verification codes are mostly unable to verify one-letter domain names or one-letter usernames, such as: i@fufuok. com or fufu@9.cn. The solution is as follows: 1. Do not judge the total length, judge the length...
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