Phone number

phone number format

Page 1 of 1

11 Replies - 965 Views - Last Post: 01 August 2009 - 09:56 PM Rate Topic: -----

#1 nashsclay  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 31-January 09

Phone number

Posted 30 July 2009 - 06:57 PM

Hello all. I am working on my site and I have the user enter in there phone number in this format xxxxxxxxx. It then goes to a mysql database. I then have the phone number appear on a different page but I want it to appear in this format (xxx-xxx-xxxx). Is this possible? I'll post a bit of code and the webpage.
Webpage with Phone number
<b><?php echo $lang['PHONE_NUMBER']; ?></b>:
<?php if ($ad['showphone'] == PHONE_SHOW) { ?>
	<?php echo $ad['phone']; ?>
<?php } else { ?>
	<i><?php echo $lang['PHONE_NOT_SHOWN']; ?></i>
<?php } ?>


Thanks all!

Is This A Good Question/Topic? 0
  • +

Replies To: Phone number

#2 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: Phone number

Posted 30 July 2009 - 07:13 PM

Yes, you can. This is one work around I can think of off the top of my head...

$phone=$lang['PHONE_NUMBER']; //For simplicity's sake

$area_code=$phone[0].$phone[1].$phone[2];

$first_three=$phone[3].$phone[4].$phone[5];

$last_four=$phone[6].$phone[7].$phone[8].$phone[9];

$phone=$area_code."-".$first_three."-".$last_four;

//$phone will now output xxx-xxx-xxxx



EDIT: Sorry, I was thinking variable string length. If you have it as a static string, then this should work.

This post has been edited by the1corrupted: 30 July 2009 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 nashsclay  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 31-January 09

Re: Phone number

Posted 30 July 2009 - 07:27 PM

I have it as big int right now is that ok or should I change it? Cuz that's the only way I could get a phone number to show all 9's like this 9999999999.
Was This Post Helpful? 0
  • +
  • -

#4 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: Phone number

Posted 30 July 2009 - 07:30 PM

No, it's fine as a big int. Numbers are numbers in PHP.

However, it is my tendancy to rip out the raw posted data (instead of storing a big-int) and instead store a string, or a varchar.
Was This Post Helpful? 0
  • +
  • -

#5 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Phone number

Posted 30 July 2009 - 07:48 PM

Kinda messy. sprintf() is really good at formatting numbers. I'm not sure what the correct format string you would need.

Substr is also a much cleaner option:
echo substr($ad['phone'],0,3)."-".substr($ad['phone'],3,3)."-".substr($ad['phone'],6)


Was This Post Helpful? 0
  • +
  • -

#6 nashsclay  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 31-January 09

Re: Phone number

Posted 30 July 2009 - 08:13 PM

Works like a charm! Thanks a ton!
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Phone number

Posted 01 August 2009 - 11:43 AM

You could also cheat and have the user enter the data into 3 different text boxes (one for area code, then the first three, then the last four), separated by a hyphen, limited in length to what they're supposed to be, checked to ensure the values are numeric, and then use sttring functions to stick them together for entry into the database.

Then if they want to edit it later, you can bring it back out of the database with explode(). This way, the user has to enter data in the format you specify, so you ensure data integrity.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5309
  • View blog
  • Posts: 27,210
  • Joined: 10-May 07

Re: Phone number

Posted 01 August 2009 - 09:00 PM

In the past when I've done this, I've gathered the phone number into three variables.

Area code - Local - Main.

3 by 3 by 4.

Then I can use sprintf() to join them, or display them with (Area Code) Local - Main

So it's sort of the opposite of what has already been suggested. But it works for me.
Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Phone number

Posted 01 August 2009 - 09:39 PM

Actually, that's roughly the same thing I suggested, no2.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5309
  • View blog
  • Posts: 27,210
  • Joined: 10-May 07

Re: Phone number

Posted 01 August 2009 - 09:43 PM

Oops.
Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Phone number

Posted 01 August 2009 - 09:55 PM

Great minds think alike and all that :)
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5309
  • View blog
  • Posts: 27,210
  • Joined: 10-May 07

Re: Phone number

Posted 01 August 2009 - 09:56 PM

Yup. Plus some can't read :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1