Skip to content

Instantly share code, notes, and snippets.

@AkostDev
Last active January 24, 2025 10:11
Show Gist options
  • Save AkostDev/07c1eb3e45d24a6bced30c9ae0ae25b1 to your computer and use it in GitHub Desktop.
Save AkostDev/07c1eb3e45d24a6bced30c9ae0ae25b1 to your computer and use it in GitHub Desktop.
PHP Phone Format
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