Format Phone Number

A simple function that I use to format phone numbers in the format (123) 456-7980 from virtually any other format. This is a great way to standardize phone data coming from html forms, as they get entered in a variety of different formats. Just store the relevant digits in the database and then format the phone number nicely in the output.

function format_phone($input){
 
	$digits = trim(ereg_replace('([^[:digit:]])','',$input));
 
	if (strlen($digits)>10) { $digits = substr($digits,-10); }
 
	return '('.substr($digits,0,3).') '.substr($digits,3,3).'-'.substr($digits,6,4);
 
}

About this entry