验证手机号或座机号的合法性

来源:互联网 发布:栅格数据 链式编码 编辑:程序博客网 时间:2024/06/09 17:08
/**
 * 验证手机号或座机号的合法性
 * @param unknown $str
 * @param string $flag  默认验证手机号的合法性
 * @return boolean
 */
function checkMobile($str, $flag = 'mobile')
{
    $match_mobile = '/1[34578]{1}\d{9}$/';
    $match_phone = '/^(0?(([1-9]\d)|([3-9]\d{2}))-?)?\d{7,8}$/';
    switch ($flag) {
        case 'mobile': //手机
            return (preg_match($match_mobile, $str)) ? true : false;
            break;
        case 'phone': //座机
            return (preg_match($match_phone, $str)) ? true : false;
            break;
        default:
            return (preg_match($match_mobile, $str)) || (preg_match($match_phone, $str));
            break;
    }
}
0 0