Last active
December 11, 2017 01:31
-
-
Save Dinir/c534f7b6e1ecdc4cbf4ddaa623899b41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 전화번호에 하이픈을 붙입니다. | |
* 02-000-0000, 02-0000-0000, 000-000-0000, 000-0000-0000, | |
* 010-000-0000, 010-0000-0000, 1000-0000 | |
* 의 형태로 전화번호를 가공하고, 숫자가 더 남았을 경우 뒤에 이어붙여 표시합니다. | |
* | |
* @param string $number 전화번호를 담은 문자열 | |
* | |
* @return string | |
*/ | |
function hyphenate_phone_number($number) | |
{ | |
$number = preg_replace( | |
'/(?|(\d{0})(1\d{3})|(02|0[13-9]\d)(\d{2,4}))(\d{4})/', | |
'$1-$2-$3', | |
$number | |
); | |
// 1000-0000의 경우 `$1`의 값을 갖지 않아 '-1000-0000' 의 형태로 가공됩니다. | |
// 여기에서 맨 앞의 하이픈을 제거합니다. | |
if($number[0] === '-') | |
$number = substr($number, 1); | |
return $number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 전화번호에 하이픈을 붙입니다. | |
* 02-000-0000, 02-0000-0000, 000-000-0000, 000-0000-0000, | |
* 010-000-0000, 010-0000-0000, 1000-0000 | |
* 의 형태로 전화번호를 가공하고, 숫자가 더 남았을 경우 뒤에 이어붙여 표시합니다. | |
* | |
* @param {string} number 전화번호를 담은 문자열 | |
* @returns {string} | |
*/ | |
function hyphenatePhoneNumber(number) { | |
return number.replace( | |
/((\d{0})(1\d{3})|(02|0[13-9]\d)(\d{2,4}))(\d{4})/, | |
function(match, p1, p2, p3, p4, p5, p6) { | |
if ( | |
typeof p4 != 'undefined' && | |
typeof p5 != 'undefined' | |
) | |
return [p4, p5, p6].join("-"); | |
if (typeof p3 != 'undefined') | |
return [p3, p6].join("-"); | |
return match; | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment