Phone Number Format
2 posts by 2 authors in: Forums > CMS Builder
Last Post: August 25, 2010 (RSS)
By Kenny - August 25, 2010
I have a spreadsheet of members I am importing into the database. Each one has a phone number and a fax displayed like this:
1234567890
No problem, except that on the website, I want to display them like this:
(123) 456-7890 or even 123-456-7890
So what do I need to do to this:
<?php echo $record['phone'] ?>
to make that happen?
Thanks,
Kenny
1234567890
No problem, except that on the website, I want to display them like this:
(123) 456-7890 or even 123-456-7890
So what do I need to do to this:
<?php echo $record['phone'] ?>
to make that happen?
Thanks,
Kenny
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)
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:
Hope this helps.
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/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/