Jump to content


Photo

**solved** Formatting for Output


  • Please log in to reply
2 replies to this topic

#1 bgirls

bgirls
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 25 March 2006 - 02:13 PM

Hi Everyone!

I have a question. How do I insert characters into a string before I echo it to the browser?

In my database, I store phone numbers as 10 digit numbers (5555555555)

Assuming that I extract a row from the database and now the following is true:

$row[phone] is equal to 5555555555

But I need to know how to insert a hyphen after the third and sixth character so I can echo it as:

555-555-5555


If anyone can help, I would appreciate it. Thanks!



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 25 March 2006 - 02:59 PM

First you need to split it into the 3 parts

$a = substr ($row['phone'], 0, 3);
$b = substr ($row['phone'], 3, 3);
$c = substr ($row['phone'], 6, 4);

echo "$a-$b-$c";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 bgirls

bgirls
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 25 March 2006 - 03:45 PM

[!--quoteo(post=358221:date=Mar 25 2006, 09:59 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 25 2006, 09:59 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
First you need to split it into the 3 parts

$a = substr ($row['phone'], 0, 3);
$b = substr ($row['phone'], 3, 3);
$c = substr ($row['phone'], 6, 4);

echo "$a-$b-$c";
[/quote]



Works like a charm. Exactly what I needed. Thanks, Barand!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users