Formatting Phone Number Function

public function format_phone($phone = '') { $return = ''; $phone = preg_replace("/[^0-9]/", "", $phone); if(strlen($phone) === 7) { $return = preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone); } elseif(strlen($phone) === 10) { $return = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone); } else { $return = trim($phone); } return $return; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.