Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Last active December 27, 2015 11:19
Show Gist options
  • Select an option

  • Save fhferreira/7318166 to your computer and use it in GitHub Desktop.

Select an option

Save fhferreira/7318166 to your computer and use it in GitHub Desktop.
Format Number
<?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