Created
October 8, 2015 23:43
-
-
Save sr2ds/0502bf2ff4e5ee1afff2 to your computer and use it in GitHub Desktop.
Função PHP para tratar números de celular padrão Brasileiro e acrescentar o 9º digito
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
$tel = $_REQUEST[tel]; | |
function trataNumero($tel) { | |
echo strlen($tel).'<br>'; | |
// seria melhor cirar uma white list. | |
// tratando manualmente | |
$tel = str_replace("-", "", $tel); | |
$tel = str_replace("(", "", $tel); | |
$tel = str_replace(")", "", $tel); | |
$tel = str_replace("_", "", $tel); | |
$tel = str_replace(" ", "", $tel); | |
$tel = str_replace("+", "", $tel); | |
//--------------------- | |
// Se nao tiver DDD e 9 digito | |
if (strlen($tel) == 8) { | |
$tel = '9'.$tel; | |
}; | |
// Se nao tiver DDD | |
if (strlen($tel) == 9) { | |
$tel = '11'.$tel; | |
}; | |
// Se tiver DDD mas nao tiver o 9 digito | |
if (strlen($tel) == 10) { | |
$inicio = substr($tel, 0, 2); | |
$fim = substr($tel, 2, 10); | |
$tel = $inicio.'9'.$fim; | |
}; | |
//verificando se é celular | |
$celReal = array ("9","8","7","6","5","4"); | |
// retirando espaços | |
$tel = trim($tel); | |
// Valida se esta com 55 | |
$ddi = strripos($tel, '55'); | |
$val_ddi = strlen($ddi); | |
if ($val_ddi != 1) { | |
$tel = '55'.$tel; | |
} | |
// Verifica se e celular mesmo | |
if (strlen($tel) == 13) { | |
$validaCel = substr($tel,5,1); | |
if (in_array($validaCel, $celReal)){ | |
return $tel; | |
} else { | |
return false; | |
} | |
} | |
} | |
echo trataNumero($tel); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment