PHP

PHP 휴대폰번호 체크 함수

은둔한량 2020. 3. 23. 14:01
반응형

php 핸드폰 번호 체크 함수

function check_phone_number($phone){
    $ph = preg_replace("/[^0-9]*/s", "", $phone);
    $ph_len = strlen($ph);
    if( $ph_len >= '8' && $ph_len <= '11' ) {
        switch( $ph_len ) {
            case 8:
                $ph = "010".$ph;
                $ph = substr($ph,0,3)."-".substr($ph,3,4)."-".substr($ph,7);
                break;
            case 9:
                $ph = "0".$ph;
                $ph = substr($ph,0,3)."-".substr($ph,3,3)."-".substr($ph,6);
                break;
            case 10:
                if( substr($ph,0,1) == '0' ) {
                    $ph = substr($ph,0,3)."-".substr($ph,3,3)."-".substr($ph,6);
                } else if( substr($ph,0,1) == '1' ) {
                $ph = "0".$ph;
                $ph = substr($ph,0,3)."-".substr($ph,3,4)."-".substr($ph,7);
                }
                break;
            case 11:
                $ph = substr($ph,0,3)."-".substr($ph,3,4)."-".substr($ph,7);
                break;
        }

        $pattern = "/^01[016789]-[0-9]{3,4}-[0-9]{4}$/";
        $rs = ( preg_match($pattern, $ph) ) ? true : false ;
        return $rs;
    }
}

$phone = "010-1234-1234";
echo check_phone_number($phone);

 

아래는 자바스크립트

function isCellPhone(p) {
    p = p.split('-').join('');
    var regPhone = /^((01[1|6|7|8|9])[1-9]+[0-9]{6,7})|(010[1-9][0-9]{7})$/;
    return regPhone.test(p);
}
반응형