Return to Snippet

Revision: 7119
at July 8, 2008 10:10 by section31


Initial Code
// Formats a phone number as (xxx) xxx-xxxx or xxx-xxxx depending on the length.
function format_phone($phone)
{
  $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 $phone;
}

Initial URL


Initial Description
Formats a phone number as (xxx) xxx-xxxx or xxx-xxxx depending on the length.

Initial Title
phone number format

Initial Tags


Initial Language
PHP