Solved

Formatting Phone Number

Posted on 2004-03-24
4
249 Views
Last Modified: 2006-11-17
Hello...

I am using PHP and an Access Database..   i have phone numbers in the databases.....

this is the format of phone number    3135551234
thiis is  the format I need  (313)555-1234

Any ideas how I can do that??

Thx
Rich
0
Comment
Question by:cookmyster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Andy
ID: 10673936
Phone numbers here in the UK vary in length but if over there they are always (3 digits)3digits-4digits then you could do this...

$number = "3135551234";
$part1 = substr($number,0,3);
$part2 = substr($number,3,3);
$part3 = substr($number,6,4);
$format = "(%s)%s-%s";
$result = sprintf($format,$part1,$part2,$part3);
echo $result;
0
 
LVL 1

Expert Comment

by:robinet02
ID: 10675131
using regexp functions :

$phone_number = "3135551234";
$phone_number = preg_replace("/(\d{3})(\d{3})(\d{4})/", "(\$1)\$2-\$3", $phone_number);

print $phone_number will now output : (313)555-1234


0
 
LVL 2

Author Comment

by:cookmyster
ID: 10865502
Hi...

Your formatting works great..one problem...    I am bringing the phone number out from a database....so I am not sure where to put the replace formatting???

Here is my code...


$result = mysql_query("select tb_name, tb_address1, tb_phonenumber from tricity_business where tb_name like '%$txtname%' and tc_id = $selcity", $linkID);


                  while ($row = mysql_fetch_row($result))
                  {
                        print"<tr>";
                        foreach($row as $field)
                        {
                              print "<td>$field</td>";
                        }
                        print "</tr>";
                  }

Thx,
Rich
0
 
LVL 1

Accepted Solution

by:
robinet02 earned 50 total points
ID: 10866247
here is the code :

$result = mysql_query("select tb_name, tb_address1, tb_phonenumber from tricity_business where tb_name like '%$txtname%' and tc_id = $selcity", $linkID);


               while ($row = mysql_fetch_row($result))
               {
                    print"<tr>";
                    foreach($row as $field)
                    {
                         print "<td>".preg_replace("/(\d{3})(\d{3})(\d{4})/", "(\$1)\$2-\$3", $field)."</td>";
                    }
                    print "</tr>";
               }

0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question