?
Solved

Formatting Phone Number

Posted on 2004-03-24
4
Medium Priority
?
250 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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

800 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