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
Return to the default flat view Create a new topic Submit Reply
Phone number formatting [message #176110] Thu, 24 November 2011 07:39 Go to previous message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma:
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]
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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: Sat Nov 30 15:57:51 GMT 2024

Total time taken to generate the page: 0.04587 seconds