Created
February 4, 2021 13:14
-
-
Save edsonmsantos/07d87c4158643e7ffd6e3c63e000c063 to your computer and use it in GitHub Desktop.
[Formatar 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 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