473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

phone number formatting

this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

Thanks
Jul 17 '05 #1
3 9894
Shay Hurley wrote:
this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

What have you tried?
What did you expect and what did your script do?

Are you sure all your numbers have exactly 11 digits?

Here's a 'brute force' method to insert dashes at the positions you
specified. You might want to turn it into a function ... and change the
positions (and quantity) of dashes to insert.

#v+
<?php
$number = '15554256987';

$number = substr($number, 0, 1) . '-' . substr($number, 1);
// now $number = '1-5554256987';

$number = substr($number, 0, 5) . '-' . substr($number, 5);
// now $number = '1-555-4256987';

$number = substr($number, 0, 9) . '-' . substr($number, 9);
// now $number = '1-555-425-6987';
?>
#v-
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Like Pedro said, a few problems...
what if I enter (555) 555-1212, or (555)-333-3321 or +1 555 333 1222 or, if
I'm from germany... 011 49 030 30 30 90 (phone number of my favorite hotel
in Berlin...)

Pedro Graca wrote:
Shay Hurley wrote:
this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

What have you tried?
What did you expect and what did your script do?

Are you sure all your numbers have exactly 11 digits?

Here's a 'brute force' method to insert dashes at the positions you
specified. You might want to turn it into a function ... and change
the positions (and quantity) of dashes to insert.

#v+
<?php
$number = '15554256987';

$number = substr($number, 0, 1) . '-' . substr($number, 1);
// now $number = '1-5554256987';

$number = substr($number, 0, 5) . '-' . substr($number, 5);
// now $number = '1-555-4256987';

$number = substr($number, 0, 9) . '-' . substr($number, 9);
// now $number = '1-555-425-6987';

#v-

Jul 17 '05 #3
Sorry, I should have been clearer in my post. I am getting a list of
numbers of different lengths, could be anything from 5 digits (directory
enquiries) to local numbers (8 digits) to international numbers (12 or
13 numbers). There is no foratting on the numbers I receive from the db
so they are just like 14567890, 11811, 15552345678, 447878787712.
Agelmar wrote:
Like Pedro said, a few problems...
what if I enter (555) 555-1212, or (555)-333-3321 or +1 555 333 1222 or, if
I'm from germany... 011 49 030 30 30 90 (phone number of my favorite hotel
in Berlin...)

Pedro Graca wrote:
Shay Hurley wrote:
this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

What have you tried?
What did you expect and what did your script do?

Are you sure all your numbers have exactly 11 digits?

Here's a 'brute force' method to insert dashes at the positions you
specified. You might want to turn it into a function ... and change
the positions (and quantity) of dashes to insert.

#v+
<?php
$number = '15554256987';

$number = substr($number, 0, 1) . '-' . substr($number, 1);
// now $number = '1-5554256987';

$number = substr($number, 0, 5) . '-' . substr($number, 5);
// now $number = '1-555-4256987';

$number = substr($number, 0, 9) . '-' . substr($number, 9);
// now $number = '1-555-425-6987';

#v-


Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
2
by: Dman | last post by:
Having trouble in Access XP. I want to display the customers name, city and phone number in a Combo Box but the but the formatting is lost – eg (123) 456-7890 is displayed as 1234567890. Any...
3
by: buck | last post by:
reports/ can we alternate formatting such as "no shading" and then "shading" for multiple records such as a phone directory in Access2000 reports
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
4
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally...
1
by: womblesjc | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
0
by: JeremyW | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
2
by: dcyale | last post by:
I have a report with the following paragraph: ="This BA presents a " & & " determination for the " & & " and associated habitat. We would appreciate you processing the biological opinion by " &...
4
by: Blue Streak | last post by:
Hello, Folks! Does anyone know of a website that lists the local phone number formats for each country? TIA...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.