Phone Number Format

2 posts by 2 authors in: Forums > CMS Builder
Last Post: August 25, 2010   (RSS)

Re: [sagentic] Phone Number Format

By Jason - August 25, 2010

Hi Kenny,

Try this.

Start by putting this function somewhere on your page (near the bottom is usually good)

<?php
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;

}
?>


This function formats either a 7 or 10 digit phone number and returns it. You can then output the formatted number in your code like this:

<?php echo format_phone($record['phone']);?>

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/