Web
[PHP] 연락처 형식 함수 format_phone
rairen
2019. 5. 2. 11:12
if(function_exists('format_phone') === flase){
/**
* @param string $phone 연락처 문자열
* @return string 반환 문자열
*/
function format_phone($phone){
$phone = preg_replace("/[^0-9]/", "", $phone);
$length = strlen($phone);
switch($length){
case 11 :
return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", "$1-$2-$3", $phone);
break;
case 10:
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
break;
default :
return $phone;
break;
}
}
}
반응형