Last active
December 27, 2015 11:19
-
-
Save fhferreira/7318166 to your computer and use it in GitHub Desktop.
Format Number
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
| <?php | |
| function ReformatPhoneNumber($number){ | |
| $match2 = preg_match('/^\d(?:[-\s]?\d){6,11}$/', $number); | |
| if( $match2 ){ | |
| echo preg_replace( '/[^0-9]/', '', $number ); | |
| }else{ | |
| throw new Exception("Invalid phone number"); | |
| } | |
| } | |
| echo ReformatPhoneNumber('012-345 69'); | |
| echo ReformatPhoneNumber('012345'); | |
| echo ReformatPhoneNumber('-012345 678'); | |
| echo ReformatPhoneNumber('01203- 34566'); | |
| echo ReformatPhoneNumber('123456678875432'); | |
| echo ReformatPhoneNumber('123 4567'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment