본문 바로가기

Develop/PHP

전화번호 국내 지역화 코드

/**
* 숫자로만 이루어진 전화번호에 dash(-)를 추가한 전화번호로 표시해줌.
* 01000000000 => 010-0000-0000
*/

if (!function_exists('localize_number')) {
function localize_number($phone)
{
$returnData = '';

$numbers_only = str_replace("-", "", $phone);

$returnData = preg_replace("/^1?(\d{3})(\d{4})(\d{4})$/", "$1-$2-$3", $numbers_only);

if ($returnData == $numbers_only) {
$returnData = preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only);
}

if ($returnData == $numbers_only) {
$returnData = preg_replace("/^1?(\d{2})(\d{4})(\d{4})$/", "$1-$2-$3", $numbers_only);
}

if ($returnData == $numbers_only) {
$returnData = preg_replace("/^1?(\d{2})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only);
}

if ($returnData == $numbers_only) {
$returnData = preg_replace("/^1?(\d{4})(\d{4})$/", "$1-$2", $numbers_only);
}

return $returnData; //preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only);
}
}