[PHP] 驗證手機號格式和郵箱格式的正則表達式


經常使用的正則,驗證是否是手機號或者郵箱

\w的意思是  [a-zA-Z0-9_] 這幾個字符

+意思是1次到多次

*意思是0次到多次

? 意思是0次到1次

<?php
class Helper{
    /**
     * 驗證手機號
     * @param $mobile
     * @return bool
     */
    public static function isMobile($mobile){
        if (empty($mobile)) {
            return false;
        }
        $eg = "/^((\(\d{2,3}\))|(\d{3}\-))?1(3|4|5|6|7|8|9)\d{9}$/";
        if (preg_match($eg, $mobile)) {
            return true;
        }
        return false;
    }

    /**
     * 驗證郵箱
     * @param $email
     * @return bool
     */
    public static function isEmail($email){
        if (empty($email)) {
            return false;
        }
        $eg = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";
        if (preg_match($eg, $email)) {
            return true;
        }
        return false;
    }
}

 


免責聲明!

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



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