PHP正则表达式判断身份

php

浏览数:359

2019-1-7

//身份证验证
$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='1515212@qq';
$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;
}