phone number formatting

This is a discussion on phone number formatting within the PHP Language forums, part of the PHP Programming Forums category; this is probably a stupid question so apologies in advance. I am trying to format a number to look like ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-17-2004
Shay Hurley
 
Posts: n/a
Default 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
Reply With Quote
  #2 (permalink)  
Old 01-17-2004
Pedro Graca
 
Posts: n/a
Default Re: phone number formatting

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 =--
Reply With Quote
  #3 (permalink)  
Old 01-17-2004
Agelmar
 
Posts: n/a
Default 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:
> 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-



Reply With Quote
  #4 (permalink)  
Old 01-17-2004
Shay Hurley
 
Posts: n/a
Default 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:
> 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-

>
>
>

Reply With Quote
  #5 (permalink)  
Old 01-18-2004
Agelmar
 
Posts: n/a
Default Re: phone number formatting

Then it becomes really easy.
$max = strlen($phonenumber); // assuming phonenumber is a string
representation
$final = ""; // final result
for ($i = 0; $i < $max; $i++)
{
if (is_int($phonenumber{$i}))
$final = $final.$phonenumber{$i};
}
(not tested, but should work...)



Shay Hurley wrote:
> 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-



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:23 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0