Return to Snippet

Revision: 57269
at May 23, 2012 21:03 by pruntrut


Initial Code
/**
* Converts phone numbers to the formatting standard
*
* @param   String   $num   A unformatted phone number
* @return  String   Returns the formatted phone number
*/
function formatPhone($num)
{
$num = preg_replace('/[^0-9]/', '', $num);
 
$len = strlen($num);
if($len == 7)
$num = preg_replace('/([0-9]{3})([0-9]{4})/', '$1-$2', $num);
elseif($len == 10)
$num = preg_replace('/([0-9]{3})([0-9]{3})([0-9]{4})/', '($1) $2-$3', $num);
 
return $num;
}
 
// echo formatPhone('1 208 - 386 2934');
// will print: (208) 386-2934 </code>

Initial URL
http://www.bluefrog.ca/2011/12/simple-phone-number-format-function/

Initial Description
Converts phone numbers to the formatting standard

Initial Title
Converts phone numbers to the formatting standard

Initial Tags
php

Initial Language
PHP