Last active
June 12, 2020 21:45
-
-
Save sdrew/ccb6bb051a6e3e115171 to your computer and use it in GitHub Desktop.
Phone Formats
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 | |
function PhoneFormat($phone) { | |
$prefix = ''; | |
$phone = preg_replace('/\D/', '', $phone); | |
$dash = strlen($phone); | |
if ($dash > 10) { | |
if (strpos($phone, '10') === 0 || strpos($phone, '11') === 0) { | |
$prefix = substr($phone, 0, 2); | |
$phone = substr_replace($phone, '', 0, 2); | |
} elseif (strpos($phone, '011') === 0) { | |
$prefix = substr($phone, 0, 3); | |
$phone = substr_replace($phone, '', 0, 3); | |
} elseif (strpos($phone, '0') === 0 || strpos($phone, '1') === 0) { | |
$prefix = substr($phone, 0, 1) . ' '; | |
$phone = substr_replace($phone, '', 0, 1); | |
} | |
$dash = strlen($phone); | |
} | |
$segment = 4; | |
while($dash > $segment) { | |
$phone = substr_replace($phone, '-', $dash - $segment, 0); | |
$dash = strpos($phone, '-'); | |
if ($segment === 4) { $segment = 3; } | |
} | |
if (substr_count($phone, '-') > 1) { | |
$dash = strpos($phone, '-'); | |
$phone = '(' . substr_replace($phone, ') ', $dash, 1); | |
if (strlen($prefix) === 0) { | |
$prefix = '+'; | |
} | |
} | |
$phone = $prefix . $phone; | |
return $phone; | |
} | |
?> |
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 | |
function PhoneFormatMX($phone) { | |
$prefix = ''; | |
$phone = preg_replace('/\D/', '', $phone); | |
$dash = strlen($phone); | |
if ($dash > 10) { | |
if (strpos($phone, '044') === 0 || strpos($phone, '045') === 0) { | |
$prefix = substr($phone, 0, 3) . ' '; | |
$phone = substr_replace($phone, '', 0, 3); | |
} elseif (strpos($phone, '00') === 0 || strpos($phone, '01') === 0) { | |
$prefix = substr($phone, 0, 2) . ' '; | |
$phone = substr_replace($phone, '', 0, 2); | |
} | |
$dash = strlen($phone); | |
} | |
$segment = 4; | |
while($dash > $segment) { | |
$phone = substr_replace($phone, '-', $dash - $segment, 0); | |
$dash = strpos($phone, '-'); | |
if ($segment === 4 && strlen($prefix) > 0) { | |
if (strpos($phone, '55') !== 0 && strpos($phone, '33') !== 0 && strpos($phone, '81') !== 0) { | |
$segment = 3; | |
} | |
} elseif ($segment === 4 && strlen($prefix) === 0 && strlen($phone) === 11) { | |
if (strpos($phone, '55') !== 0 && strpos($phone, '33') !== 0 && strpos($phone, '81') !== 0) { | |
$segment = 3; | |
} | |
} | |
} | |
if (substr_count($phone, '-') > 1) { | |
$dash = strpos($phone, '-'); | |
$phone = '(' . substr_replace($phone, ') ', $dash, 1); | |
if (strlen($prefix) === 0) { | |
$prefix = '+'; | |
} | |
} | |
$phone = $prefix . $phone; | |
return $phone; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment