Connecting Tech Pros Worldwide Help | Site Map

phone number formatting

  #1  
Old July 17th, 2005, 03:28 AM
Shay Hurley
Guest
 
Posts: n/a
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
  #2  
Old July 17th, 2005, 03:28 AM
Pedro Graca
Guest
 
Posts: n/a

re: phone number formatting


Shay Hurley wrote:[color=blue]
> 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.[/color]


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 =--
  #3  
Old July 17th, 2005, 03:28 AM
Agelmar
Guest
 
Posts: n/a

re: phone number formatting


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:[color=blue]
> Shay Hurley wrote:[color=green]
>> 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.[/color]
>
>
> 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';[color=green]
>>[/color]
> #v-[/color]


  #4  
Old July 17th, 2005, 03:29 AM
Shay Hurley
Guest
 
Posts: n/a

re: phone number formatting


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:[color=blue]
> 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:
>[color=green]
>>Shay Hurley wrote:
>>[color=darkred]
>>>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.[/color]
>>
>>
>>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-[/color]
>
>
>[/color]
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting phone number in Access Report dcyale answers 2 September 21st, 2006 03:37 PM
Phone Number FORMATTING czbacnik@gmail.com answers 2 April 12th, 2006 10:25 PM
Formatting phone number of a DetailsView control womblesjc@royell.org answers 1 March 22nd, 2006 11:55 PM
phone number regex Brian Henry answers 4 November 21st, 2005 06:41 PM