php正則表達式驗證手機/固定電話/郵箱/身份證/銀行卡自定義函數


/**
 * 驗證手機號碼格式
 * @param string $phone 手機號
 * @return boolean
 */
function is_phone($phone) {
    $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/";
    if (preg_match($chars, $phone)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 驗證固定電話格式
 * @param string $tel 固定電話
 * @return boolean
 */
function is_tel($tel) {
    $chars = "/^([0-9]{3,4}-)?[0-9]{7,8}$/";
    if (preg_match($chars, $tel)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 驗證郵箱格式
 * @param string $email 郵箱
 * @return boolean
 */
function is_email($email) {
    $chars = "/^[0-9a-zA-Z]+(?:[\_\.\-][a-z0-9\-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\.[a-zA-Z]+$/i";
    if (preg_match($chars, $email)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 驗證身份證號碼格式
 * @param string $id_card 身份證號碼
 * @return boolean
 */
function is_idcard($id_card) {
    $chars = "/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/";
    if (preg_match($chars, $id_card)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 驗證銀行卡號碼格式
 * @param string $id_card 銀行卡號碼
 * @return boolean
 */
function is_bank($bank) {
    $chars = "/^(\d{16}|\d{19}|\d{17})$/";
    if (preg_match($chars, $bank)) {
        return true;
    } else {
        return false;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM