Last active
January 24, 2025 10:11
-
-
Save AkostDev/07c1eb3e45d24a6bced30c9ae0ae25b1 to your computer and use it in GitHub Desktop.
PHP Phone Format
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) { | |
$phone = trim($phone); | |
return preg_replace( | |
array( | |
'/[+]?([7|8])[-|\s]?\([-|\s]?(\d{3})[-|\s]?\)[-|\s]?(\d{3})[-|\s]?(\d{2})[-|\s]?(\d{2})/', | |
'/[+]?([7|8])[-|\s]?(\d{3})[-|\s]?(\d{3})[-|\s]?(\d{2})[-|\s]?(\d{2})/', | |
'/[+]?([7|8])[-|\s]?\([-|\s]?(\d{4})[-|\s]?\)[-|\s]?(\d{2})[-|\s]?(\d{2})[-|\s]?(\d{2})/', | |
'/[+]?([7|8])[-|\s]?(\d{4})[-|\s]?(\d{2})[-|\s]?(\d{2})[-|\s]?(\d{2})/', | |
'/[+]?([7|8])[-|\s]?\([-|\s]?(\d{4})[-|\s]?\)[-|\s]?(\d{3})[-|\s]?(\d{3})/', | |
'/[+]?([7|8])[-|\s]?(\d{4})[-|\s]?(\d{3})[-|\s]?(\d{3})/', | |
), | |
array( | |
'+7 ($2) $3-$4-$5', | |
'+7 ($2) $3-$4-$5', | |
'+7 ($2) $3-$4-$5', | |
'+7 ($2) $3-$4-$5', | |
'+7 ($2) $3-$4', | |
'+7 ($2) $3-$4', | |
), | |
$phone | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment