Skip to content

Instantly share code, notes, and snippets.

@edsonmsantos
Created February 4, 2021 13:14
Show Gist options
  • Save edsonmsantos/07d87c4158643e7ffd6e3c63e000c063 to your computer and use it in GitHub Desktop.
Save edsonmsantos/07d87c4158643e7ffd6e3c63e000c063 to your computer and use it in GitHub Desktop.
[Formatar telefone]
function formatPhone($phone)
{
$formatedPhone = preg_replace('/[^0-9]/', '', $phone);
$matches = [];
preg_match('/^([0-9]{2})([0-9]{4,5})([0-9]{4})$/', $formatedPhone, $matches);
if ($matches) {
return '('.$matches[1].') '.$matches[2].'-'.$matches[3];
}
return $phone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment