Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created March 16, 2014 19:09
Show Gist options
  • Save wpsmith/9588238 to your computer and use it in GitHub Desktop.
Save wpsmith/9588238 to your computer and use it in GitHub Desktop.
PHP: Format US Phone Number to always be the same.
<?php
$formatted_phone = preg_replace( '~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '($1) $2-$3', $unformatted_number );
$formatted_phone = preg_replace( '~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1.$2.$3', $unformatted_number );
$formatted_phone = preg_replace( '~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1-$2-$3', $unformatted_number );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment