Created
April 19, 2020 04:41
-
-
Save brunocmoraes/9bf9cc094ecd51d7dfae8c1be6c2ea8f to your computer and use it in GitHub Desktop.
Função PHP para formatar números de telefone
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
function telefone($n) | |
{ | |
$tam = strlen(preg_replace("/[^0-9]/", "", $n)); | |
if ($tam == 13) { | |
// COM CÓDIGO DE ÁREA NACIONAL E DO PAIS e 9 dígitos | |
return "+".substr($n, 0, $tam-11)." (".substr($n, $tam-11, 2).") ".substr($n, $tam-9, 5)."-".substr($n, -4); | |
} | |
if ($tam == 12) { | |
// COM CÓDIGO DE ÁREA NACIONAL E DO PAIS | |
return "+".substr($n, 0, $tam-10)." (".substr($n, $tam-10, 2).") ".substr($n, $tam-8, 4)."-".substr($n, -4); | |
} | |
if ($tam == 11) { | |
// COM CÓDIGO DE ÁREA NACIONAL e 9 dígitos | |
return " (".substr($n, 0, 2).") ".substr($n, 2, 5)."-".substr($n, 7, 11); | |
} | |
if ($tam == 10) { | |
// COM CÓDIGO DE ÁREA NACIONAL | |
return " (".substr($n, 0, 2).") ".substr($n, 2, 4)."-".substr($n, 6, 10); | |
} | |
if ($tam <= 9) { | |
// SEM CÓDIGO DE ÁREA | |
return substr($n, 0, $tam-4)."-".substr($n, -4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment