PHP常用正則驗證
阿新 • • 發佈:2018-12-30
//正則驗證手機號 正確返回 true function preg_mobile($mobile) { if(preg_match("/^1[34578]\d{9}$/", $mobile)) { return TRUE; } else { return FALSE; } } //驗證電話號碼 function preg_tel($tel) { if(preg_match("/^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/", $tel)) { return TRUE; } else { return FALSE; } } //驗證身份證號(15位或18位數字) function preg_idcard($idcard) { if(preg_match("/^\d{15}|\d{18}$/", $idcard)) { return TRUE; } else { return FALSE; } } //驗證是否是數字(這裡小數點會認為是字元) function preg_digit($digit) { if(preg_match("/^\d*$/", $digit)) { return TRUE; } else { return FALSE; } } //驗證是否是數字(可帶小數點的數字) function preg_num($num) { if(is_numeric($num)) { return TRUE; } else { return FALSE; } } //驗證由數字、26個英文字母或者下劃線組成的字串 function preg_str($str) { if(preg_match("/^\w+$/", $str)) { return TRUE; } else { return FALSE; } } //驗證使用者密碼(以字母開頭,長度在6-18之間,只能包含字元、數字和下劃線) function preg_password($str) { if(preg_match("/^[a-zA-Z]\w{5,17}$/", $str)) { return TRUE; } else { return FALSE; } } //驗證漢字 function preg_chinese($str) { if(preg_match("/^[\u4e00-\u9fa5],{0,}$/", $str)) { return TRUE; } else { return FALSE; } } //驗證Email地址 function preg_email($email) { if(preg_match("/^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $email)) { return TRUE; } else { return FALSE; } } //驗證網址URL function preg_link($url) { if(preg_match("/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is", $url)) { return TRUE; } else { return FALSE; } } //騰訊QQ號 function preg_qq($qq) { if(preg_match("/^[1-9][0-9]{4,}$/", $qq)) { return TRUE; } else { return FALSE; } } //驗證中國郵政編碼 6位數字 function preg_post($post) { if(preg_match("/^[1-9]\d{5}(?!\d)$/", $post)) { return TRUE; } else { return FALSE; } } //驗證IP地址 function preg_ip($ip) { if(preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) { return TRUE; } else { return FALSE; } }