1. 程式人生 > >PHP正則表示式判斷身份

PHP正則表示式判斷身份


//身份證驗證
$id_card = '';
$preg_card="/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i";
                        if(preg_match($preg_card,$id_card)){
                            return true;
                        }

//郵箱驗證
$email='[email protected]';
$preg_email='/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+[-.])+([a-z]{2,5})$/ims';
if(preg_match($preg_email,$email)){
    echo 1;
}else{
    echo 0;
}

//驗證手機號
$phone='13651121610';
$preg_phone='/^1[345789]\d{9}$/ims';
if(preg_match($preg_phone,$phone)){
    echo 1;
}else{
    echo 0;
}

//驗證港澳臺+內地的
$phone='13651121610';
$preg_phone='/^[1][3-9]\d{9}$|^([6|9])\d{7}$|^[0][9]\d{8}$|^[6]([8|6])\d{5}$/';
if(preg_match($preg_phone,$phone)){
    echo 1;
}else{
    echo 0;
}

//驗證固定號碼
$call='02312345678';
$preg_call='/^(0\d{2,3})?(\d{7,8})$/ims';
if(preg_match($preg_call,$call)){
    echo 1;
}else{
    echo 0;
}

//驗證只包含中英文的名字
$name='dfdd';
$preg_name='/^[\x{4e00}-\x{9fa5}]{2,10}$|^[a-zA-Z\s]*[a-zA-Z\s]{2,20}$/isu';
if(preg_match($preg_name,$name)){
    echo 1;
}else{
    echo 0;
}

//驗證銀行卡號
$bankcard='';
$preg_bankcard='/^(\d{15}|\d{16}|\d{19})$/isu';
if(preg_match($preg_card,$bankcard)){
    echo 1;
}else{
    echo 0;
}

//驗證QQ號碼
$QQ='';
$preg_QQ='/^\d{5,12}$/isu';
if(preg_match($preg_QQ,$QQ)){
    echo 1;
}else{
    echo 0;
}

//驗證微訊號
$wechat='cdxcfvsd515d5___1';
$preg_wechat='/^[_a-zA-Z0-9]{5,19}+$/isu';
if(preg_match($preg_wechat,$wechat)){
    echo 1;
}else{
    echo 0;
}

//驗證特殊符號(如需要驗證其他字元,自行轉義 "\X" 新增)
$spacial='^%**Y*(';
$preg_spacial="/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\/|\;|\\' | \`|\-|\=|\\\|\|/isu";
if(preg_match($preg_spacial,$spacial)){
    echo 1;
}else{
    echo 0;
}