To format a phone number before displaying it on the page, you can use the following function. This function insert spaces so that the phone number becomes easier to read by the user. You can modify the function to replace the spaces with hyphen or parenthesis if you want.
//FUNCTION TO FORMAT PHONE NUMBERS function formatPhone($num) { $num = preg_replace('/[^0-9]/', '', $num); $len = strlen($num); if($len<=6) { $num = preg_replace('/([0-9]{3})([0-9]{3})/', ' $1 $2', $num); } else if(($len>6)&&($len<=9)) { $num = preg_replace('/([0-9]{3})([0-9]{3})([0-9]{1})/', ' $1 $2 $3', $num); } else if($len == 10) { $num = preg_replace('/([0-9]{3})([0-9]{3})([0-9]{4})/', ' $1 $2 $3', $num); } else if($len>10) { $num = preg_replace('/([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{1})/', ' $1 $2 $3 $4', $num); } return $num; } //END OF FUNCTION TO FORMAT PHONE NUMBERS
Reblogged this on Dinesh Ram Kali..
LikeLike