FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Phone number formatting
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Phone number formatting [message #176110] Thu, 24 November 2011 07:39 Go to next message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
Member
So, I have a list of phone numbers in various formats I.e. the users
have written them in different styles, with and without dashes, spaces
and so on.

Now I want to format them. I have a text file with all the area codes
for Sweden, so I remove any non-digit character from the phone number
and then I parse out the area code. I am now left with the phone
number. An example:

In-data: 0703051947
Parse out area code: 070
Phone number: 3051947

Now, I have an array with formattings, that looks like this:

$formats = array("### ##", "## ## ##", "### ## ##");

The phone number above is never more than 7 digits long (as far as I
know) in Sweden, so these formats should suffice. Let me explain, in
the array, we have formats for 5, 6 and 7 digit phone numbers, set up
in a specific way. So this phone number in my example above is 7
digits, so it should be formatted as such:

305 19 47

With me so far? What I don't know is exactly how to do it. This is my
code thus far:

$number = "3051947";
foreach ($formats as $f){
$chars = strlen(preg_replace("/[^#]/", "", $f));
}

So, $chars contains the number of digits the given format takes, and
then I want to compare it to how many digits I have in $number.

My problem:

I'm not sure where to start in converting $number into a string that
matches the matching format, I.e. replace the first "#" with 3, the
second with 0 and so on.

Any hints or pointers?


--
Sandman[.net]
Re: Phone number formatting [message #176113 is a reply to message #176110] Thu, 24 November 2011 08:55 Go to previous messageGo to next message
Jonathan Stein is currently offline  Jonathan Stein
Messages: 43
Registered: September 2010
Karma: 0
Member
Den 24-11-2011 08:39, Sandman skrev:

> $number = "3051947";

I would do it the "boring" way:

if (strlen($number) > 5) $number = substr_replace($number, ' ', -4, 0);
$number = substr_replace($number, ' ', -2, 0);

Regards

Jonathan
Re: Phone number formatting [message #176114 is a reply to message #176110] Thu, 24 November 2011 09:43 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 24/11/2011 8:39, Sandman escribió/wrote:
> I have an array with formattings, that looks like this:
>
> $formats = array("### ##", "## ## ##", "### ## ##");
>
> The phone number above is never more than 7 digits long (as far as I
> know) in Sweden, so these formats should suffice. Let me explain, in
> the array, we have formats for 5, 6 and 7 digit phone numbers, set up
> in a specific way. So this phone number in my example above is 7
> digits, so it should be formatted as such:
>
> 305 19 47

It's an interesting problem. I've came up with this proof of concept:

function format_phone_number($number){
$formats = array(
5 => "### ##",
6 => "## ## ##",
7 => "### ## ##"
);

$len = strlen($number);

if( isset($formats[$len]) ){
$format = strtr($formats[$len], array('#' => '%s'));
$digits = str_split($number);
return vsprintf($format, $digits);
}else{
return $number;
}
}

Basically, it converts your expression into a printf-compatible format.

Of course, it'd be easier to use such expression from the beginning:

function format_phone_number($number){
$formats = array(
5 => "%s%s%s %s%s",
6 => "%s%s %s%s %s%s",
7 => "%s%s%s %s%s %s%s"
);

$len = strlen($number);

if( isset($formats[$len]) ){
$digits = str_split($number);
return vsprintf($formats[$len], $digits);
}else{
return $number;
}
}

You could replace %s with %d if you validate that $number is an integer.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Phone number formatting [message #176120 is a reply to message #176113] Thu, 24 November 2011 12:27 Go to previous messageGo to next message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
Member
In article <4ece067f$0$293$14726298(at)news(dot)sunsite(dot)dk>,
Jonathan Stein <jstein(at)image(dot)dk> wrote:

> Den 24-11-2011 08:39, Sandman skrev:
>
>> $number = "3051947";
>
> I would do it the "boring" way:
>
> if (strlen($number) > 5) $number = substr_replace($number, ' ', -4, 0);
> $number = substr_replace($number, ' ', -2, 0);

Yeah, I really like to have formats, so I can have different for
different countries and such.





--
Sandman[.net]
Re: Phone number formatting [message #176122 is a reply to message #176114] Thu, 24 November 2011 13:22 Go to previous messageGo to next message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
Member
In article <jal3k2$61m$1(at)dont-email(dot)me>,
"Álvaro G. Vicario" <alvaro(dot)NOSPAMTHANX(at)demogracia(dot)com(dot)invalid>
wrote:

> El 24/11/2011 8:39, Sandman escribió/wrote:
>> I have an array with formattings, that looks like this:
>>
>> $formats = array("### ##", "## ## ##", "### ## ##");
>>
>> The phone number above is never more than 7 digits long (as far as I
>> know) in Sweden, so these formats should suffice. Let me explain, in
>> the array, we have formats for 5, 6 and 7 digit phone numbers, set up
>> in a specific way. So this phone number in my example above is 7
>> digits, so it should be formatted as such:
>>
>> 305 19 47
>
> It's an interesting problem. I've came up with this proof of concept:
>
> function format_phone_number($number){
> $formats = array(
> 5 => "### ##",
> 6 => "## ## ##",
> 7 => "### ## ##"
> );
>
> $len = strlen($number);
>
> if( isset($formats[$len]) ){
> $format = strtr($formats[$len], array('#' => '%s'));
> $digits = str_split($number);
> return vsprintf($format, $digits);
> }else{
> return $number;
> }
> }
>
> Basically, it converts your expression into a printf-compatible format.
>
> Of course, it'd be easier to use such expression from the beginning:
>
> function format_phone_number($number){
> $formats = array(
> 5 => "%s%s%s %s%s",
> 6 => "%s%s %s%s %s%s",
> 7 => "%s%s%s %s%s %s%s"
> );
>
> $len = strlen($number);
>
> if( isset($formats[$len]) ){
> $digits = str_split($number);
> return vsprintf($formats[$len], $digits);
> }else{
> return $number;
> }
> }
>
> You could replace %s with %d if you validate that $number is an integer.

Thank you! Your suggestions sparked some ideas that I followed,
especially the printf-friendly conversion. I also changed the formats
to include the area code, as such:

"A-## ## ##"

And replaced it all to

"%s-%s%s %s%s %s%s"

%s is necessary since %d will strip leading zeroes.


--
Sandman[.net]
Re: Phone number formatting [message #176123 is a reply to message #176110] Thu, 24 November 2011 15:17 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 24 Nov 2011 08:39:17 +0100, Sandman wrote:

> So, I have a list of phone numbers in various formats I.e. the users
> have written them in different styles, with and without dashes, spaces
> and so on.
>
> Now I want to format them. I have a text file with all the area codes
> for Sweden, so I remove any non-digit character from the phone number
> and then I parse out the area code. I am now left with the phone number.
> An example:
>
> In-data: 0703051947
> Parse out area code: 070
> Phone number: 3051947
>
> Now, I have an array with formattings, that looks like this:
>
> $formats = array("### ##", "## ## ##", "### ## ##");
>
> The phone number above is never more than 7 digits long (as far as I
> know) in Sweden, so these formats should suffice. Let me explain, in the
> array, we have formats for 5, 6 and 7 digit phone numbers, set up in a
> specific way. So this phone number in my example above is 7 digits, so
> it should be formatted as such:
>
> 305 19 47
>
> With me so far? What I don't know is exactly how to do it. This is my
> code thus far:
>
> $number = "3051947";
> foreach ($formats as $f){
> $chars = strlen(preg_replace("/[^#]/", "", $f));
> }

Not sure why you want to use preg_replace for this

switch(strlen($number)) {
case 5: $chars = substr($number,0,3) . " " . substr($number,3,2); break;
case 6: $chars = substr($number,0,2) . " " . substr($number,2,2) . " " .
substr($number,4,2); break;
case 7: $chars = substr($number,0,3) . " " . substr($number,3,2) . " " .
substr($number,5,2); break;
default: // handle any unexpected number lengths
}

Some code purists say that you should use multiple if / if else / else
instead of switch, make your own mind up.

$digits = strlen($number);
if ($digits == 5) {
$chars = substr($number,0,3) . " " . substr($number,3,2);
}
else if ($digits == 6) {
$chars = substr($number,0,2) . " " . substr($number,2,2) . " " . substr
($number,4,2);}
else if ($digits == 7) {
$chars = substr($number,0,3) . " " . substr($number,3,2) . " " . substr
($number,5,2);
}
else {
// handle any unexpected number lengths
}

Rgds

Denis McMahon
Re: Phone number formatting [message #176124 is a reply to message #176120] Thu, 24 November 2011 16:45 Go to previous messageGo to next message
Jonathan Stein is currently offline  Jonathan Stein
Messages: 43
Registered: September 2010
Karma: 0
Member
Den 24-11-2011 13:27, Sandman skrev:

> Yeah, I really like to have formats, so I can have different for
> different countries and such.

The funny approach (if you're payed by the hour) would be to invent a
general phone number format specification and write a parser to match
the number against possible formats.

On the other hand, I understood that you have already country specific
code to extract the area code, so two more lines of code, and the job is
done...

Regards

Jonathan
Re: Phone number formatting [message #176126 is a reply to message #176123] Fri, 25 November 2011 08:09 Go to previous messageGo to next message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
Member
In article <4ece601a$0$28675$a8266bb1(at)newsreader(dot)readnews(dot)com>,
Denis McMahon <denismfmcmahon(at)gmail(dot)com> wrote:

> On Thu, 24 Nov 2011 08:39:17 +0100, Sandman wrote:
>
>> So, I have a list of phone numbers in various formats I.e. the users
>> have written them in different styles, with and without dashes, spaces
>> and so on.
>>
>> Now I want to format them. I have a text file with all the area codes
>> for Sweden, so I remove any non-digit character from the phone number
>> and then I parse out the area code. I am now left with the phone number.
>> An example:
>>
>> In-data: 0703051947
>> Parse out area code: 070
>> Phone number: 3051947
>>
>> Now, I have an array with formattings, that looks like this:
>>
>> $formats = array("### ##", "## ## ##", "### ## ##");
>>
>> The phone number above is never more than 7 digits long (as far as I
>> know) in Sweden, so these formats should suffice. Let me explain, in the
>> array, we have formats for 5, 6 and 7 digit phone numbers, set up in a
>> specific way. So this phone number in my example above is 7 digits, so
>> it should be formatted as such:
>>
>> 305 19 47
>>
>> With me so far? What I don't know is exactly how to do it. This is my
>> code thus far:
>>
>> $number = "3051947";
>> foreach ($formats as $f){
>> $chars = strlen(preg_replace("/[^#]/", "", $f));
>> }
>
> Not sure why you want to use preg_replace for this
>
> switch(strlen($number)) {
> case 5: $chars = substr($number,0,3) . " " . substr($number,3,2); break;
> case 6: $chars = substr($number,0,2) . " " . substr($number,2,2) . " " .
> substr($number,4,2); break;
> case 7: $chars = substr($number,0,3) . " " . substr($number,3,2) . " " .
> substr($number,5,2); break;
> default: // handle any unexpected number lengths
> }
>
> Some code purists say that you should use multiple if / if else / else
> instead of switch, make your own mind up.
>
> $digits = strlen($number);
> if ($digits == 5) {
> $chars = substr($number,0,3) . " " . substr($number,3,2);
> }
> else if ($digits == 6) {
> $chars = substr($number,0,2) . " " . substr($number,2,2) . " " . substr
> ($number,4,2);}
> else if ($digits == 7) {
> $chars = substr($number,0,3) . " " . substr($number,3,2) . " " . substr
> ($number,5,2);
> }
> else {
> // handle any unexpected number lengths
> }
>
> Rgds
>
> Denis McMahon

That... totally removed my handy array of formats :)

I kind of want that one :)



--
Sandman[.net]
Re: Phone number formatting [message #176151 is a reply to message #176110] Sat, 26 November 2011 19:39 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 24 Nov 2011 08:39:17 +0100, Sandman wrote:

> So, I have a list of phone numbers in various formats I.e. the users
> have written them in different styles, with and without dashes, spaces
> and so on.

I was thinking about this while I was staying away last night (the bed
wasn't what I'm used to) and came up with the following (displays it's
own source).

http://www.sined.co.uk/tmp/telnos.php

Might be of interest.

Rgds

Denis McMahon
Re: Phone number formatting [message #176156 is a reply to message #176151] Mon, 28 November 2011 07:39 Go to previous message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
Member
In article <4ed14055$0$29353$a8266bb1(at)newsreader(dot)readnews(dot)com>,
Denis McMahon <denismfmcmahon(at)gmail(dot)com> wrote:

> On Thu, 24 Nov 2011 08:39:17 +0100, Sandman wrote:
>
>> So, I have a list of phone numbers in various formats I.e. the users
>> have written them in different styles, with and without dashes, spaces
>> and so on.
>
> I was thinking about this while I was staying away last night (the bed
> wasn't what I'm used to) and came up with the following (displays it's
> own source).
>
> http://www.sined.co.uk/tmp/telnos.php
>
> Might be of interest.

Indeed. I will be doing something similar. I'm loading the area codes
from a file, so I could as easily load formats from the same file, so
I could load different data depending on country.


--
Sandman[.net]
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: session handler auto log out
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 21 21:50:31 GMT 2024

Total time taken to generate the page: 0.04119 seconds