Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #3905 > unrolled thread
| Started by | Sandman <mr@sandman.net> |
|---|---|
| First post | 2011-11-24 08:39 +0100 |
| Last post | 2011-11-28 08:39 +0100 |
| Articles | 10 — 4 participants |
Back to article view | Back to comp.lang.php
Phone number formatting Sandman <mr@sandman.net> - 2011-11-24 08:39 +0100
Re: Phone number formatting Jonathan Stein <jstein@image.dk> - 2011-11-24 09:55 +0100
Re: Phone number formatting Sandman <mr@sandman.net> - 2011-11-24 13:27 +0100
Re: Phone number formatting Jonathan Stein <jstein@image.dk> - 2011-11-24 17:45 +0100
Re: Phone number formatting "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-11-24 10:43 +0100
Re: Phone number formatting Sandman <mr@sandman.net> - 2011-11-24 14:22 +0100
Re: Phone number formatting Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-24 15:17 +0000
Re: Phone number formatting Sandman <mr@sandman.net> - 2011-11-25 09:09 +0100
Re: Phone number formatting Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-26 19:39 +0000
Re: Phone number formatting Sandman <mr@sandman.net> - 2011-11-28 08:39 +0100
| From | Sandman <mr@sandman.net> |
|---|---|
| Date | 2011-11-24 08:39 +0100 |
| Subject | Phone number formatting |
| Message-ID | <mr-CCE3D7.08391724112011@News.Individual.NET> |
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]
[toc] | [next] | [standalone]
| From | Jonathan Stein <jstein@image.dk> |
|---|---|
| Date | 2011-11-24 09:55 +0100 |
| Message-ID | <4ece067f$0$293$14726298@news.sunsite.dk> |
| In reply to | #3905 |
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
[toc] | [prev] | [next] | [standalone]
| From | Sandman <mr@sandman.net> |
|---|---|
| Date | 2011-11-24 13:27 +0100 |
| Message-ID | <mr-C4900A.13270024112011@News.Individual.NET> |
| In reply to | #3908 |
In article <4ece067f$0$293$14726298@news.sunsite.dk>, Jonathan Stein <jstein@image.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]
[toc] | [prev] | [next] | [standalone]
| From | Jonathan Stein <jstein@image.dk> |
|---|---|
| Date | 2011-11-24 17:45 +0100 |
| Message-ID | <4ece74c1$0$286$14726298@news.sunsite.dk> |
| In reply to | #3915 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> |
|---|---|
| Date | 2011-11-24 10:43 +0100 |
| Message-ID | <jal3k2$61m$1@dont-email.me> |
| In reply to | #3905 |
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
--
[toc] | [prev] | [next] | [standalone]
| From | Sandman <mr@sandman.net> |
|---|---|
| Date | 2011-11-24 14:22 +0100 |
| Message-ID | <mr-96F3D1.14223724112011@News.Individual.NET> |
| In reply to | #3909 |
In article <jal3k2$61m$1@dont-email.me>,
"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.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]
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-11-24 15:17 +0000 |
| Message-ID | <4ece601a$0$28675$a8266bb1@newsreader.readnews.com> |
| In reply to | #3905 |
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
[toc] | [prev] | [next] | [standalone]
| From | Sandman <mr@sandman.net> |
|---|---|
| Date | 2011-11-25 09:09 +0100 |
| Message-ID | <mr-58EBEF.09091925112011@News.Individual.NET> |
| In reply to | #3918 |
In article <4ece601a$0$28675$a8266bb1@newsreader.readnews.com>,
Denis McMahon <denismfmcmahon@gmail.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]
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-11-26 19:39 +0000 |
| Message-ID | <4ed14055$0$29353$a8266bb1@newsreader.readnews.com> |
| In reply to | #3905 |
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
[toc] | [prev] | [next] | [standalone]
| From | Sandman <mr@sandman.net> |
|---|---|
| Date | 2011-11-28 08:39 +0100 |
| Message-ID | <mr-255D45.08390028112011@News.Individual.NET> |
| In reply to | #3946 |
In article <4ed14055$0$29353$a8266bb1@newsreader.readnews.com>, Denis McMahon <denismfmcmahon@gmail.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]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web