Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #3917
| From | Sandman <mr@sandman.net> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Phone number formatting |
| Date | 2011-11-24 14:22 +0100 |
| Message-ID | <mr-96F3D1.14223724112011@News.Individual.NET> (permalink) |
| References | <mr-CCE3D7.08391724112011@News.Individual.NET> <jal3k2$61m$1@dont-email.me> |
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]
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web