Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3905

Phone number formatting

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Sandman <mr@sandman.net>
Newsgroups comp.lang.php
Subject Phone number formatting
Date Thu, 24 Nov 2011 08:39:17 +0100
Lines 47
Message-ID <mr-CCE3D7.08391724112011@News.Individual.NET> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace individual.net NUsJnNnGjkMw+fkROGYoVwggJbu+BUh8tMu5nhazHL2V7AFmg=
X-Orig-Path mr
Cancel-Lock sha1:9nYK/tyzcZPTcaQ0H1JJqqBZsdo=
User-Agent MT-NewsWatcher/3.5.2 (Intel Mac OS X)
X-Face $@,Vfa$,)%=Qa7L]y)&oZj_\EiHc}}A<Y3TvbI2&|e"bE9zc[o<ThMgB4%*L$b1YsNl!/ <lHO$>f0Bei"4a_%)"c6TQ+P/:53>;PNGuWUmkqyeN-qM65foJ[;T_(k;>]&G\T4Lhm:2 ujye2_,iUJFE;NZn>y;.|-hl7g~bIOF1qG\o<?]4mXkW*mT3]{Bn&VwP7(M0uYnGA!V!? {"y?BkBDW6e-.=I5
X-Killfiled yttrx, gallopinginsanity.com, Mark Kent, Maverick, Nasht.n, NRen2, MuahMan, weedhopper, PC Guy, Brian, nospam@nospam.com, Oxford, Jim Lee Jr., Mocassin Joe, zara, Chance Furlong, Robert Whelan, jt2002a@hotmail.com
Xref x330-a1.tempe.blueboxinc.net comp.lang.php:3905

Show key headers only | View raw


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]

Back to comp.lang.php | Previous | Next — Next in thread | Find similar | Unroll thread


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