Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Formatting Phone Number

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 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