Solved

Formatting Phone Number

Posted on 2004-03-24
4
243 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
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Reapz
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Link SQL table to Webpage 9 39
html input clean up 3 32
How Can I Use otf Custom Font with TCPDF 7 11
Datepicker in PHP 9 21
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now