tags:

views:

230

answers:

5

i need to format mobile numbers. e.g.

+61421 123 123
0421 123 123
0061421123123
0421 123123

1) into this type of string to save in the dbf

0421123123

2) then display it in this format

0421 123 123

any suggestion on the most effective way to format the numbers?

A: 

This not really suitable for regex. How about (a) remove non digits, (b) reject if not 10 chars, (c) format using substrings.

Ewan Todd
+4  A: 
  • remove non-digits from the input string
  • slice last 9 digits
  • prepend zero and store

To display:

  • insert spaces where appropriate

Or maybe you could just store already formatted string into the db.

edit (to answer question in comment). This seem to do job just fine:

$s = '421123123';
$formatted = '0'.chunk_split($s, 3, ' ');
SilentGhost
do you know of any function that would insert a space where appropriate all in one go? i was thinking of chunk_split() but i would have to run it twice on each number...
ondrobaco
This doesn't check if the numbers do in fact conform to the rules (leading national prefix (61) or 0, then exactly 9 digits). At least that's what the rules appear to be judging from the comments. The original question is rather vague...
Tim Pietzcker
@Tim: I don't think that it's possible to protect oneself from all possible users' idiosyncrasies.
SilentGhost
is there any other of formatting the string for the display? the numbers in the database need to be stored with 0 as the first num.
ondrobaco
@ondrobaco: just `substr` the subject
SilentGhost
+1  A: 

Here's a PHP function that will properly format a phone number, so that the first space is after the area code and the rest is split evenly into two parts. The code here is specific for finnish areacodes (and doesn't take country codes into account) but modify as needed:

// Formats 0451234567 => 045 1234 567
function format_phone($phone) {
    // List your area codes here
    $phone = preg_replace('/(02|03|05|06|08|09|013|014|015|016|017|018|019|020|040|041|042|043|044|045|046|050)/', '$1 ', $phone);
    list($d, $p) = explode(' ', $phone);
    $split_point = ceil(strlen($p) / 2);
    $p = substr($p, 0, $split_point).' '.substr($p, $split_point);
    return $d.' '.$p;
}

For the saving into database part, just strip characters that are non numeric, substr to specific length and insert into db. If your country's phone numbers are all similarly formatted and 9 digits long (without the prefix 0), you can just take the last 9 digits from the phone number using:

$phone = substr($phone, strlen($phone) - 9):

And add 0 to the front. So this effectively turns the country code into 0 but only works if all phone numbers are the same length.

Tatu Ulmanen
A: 
^(?:\+61|0061|0)(\d{3})\s*(\d{3})\s*(\d{3})$

This regex will match any of the examples, capturing 421 in backreference no.1, 123 in no. 2, and 123 in no. 3.

So, if you use 0\1\2\3 as your replace string, you get 0421123123, and if you use 0\1 \2 \3, you get 0421 123 123.

Tim Pietzcker
it has ot apply to all numbers... i used this particular number just as an example.
ondrobaco
It does apply to all numbers, except that only the prefixes +61, 0061 or 0 are allowed (and the remainder has exactly 9 digits).
Tim Pietzcker
A: 

Here are some valid formats for Australian mobile phone numbers:

  • 0401 123 234
  • 041 123 3456
  • 0412 23 23 34 (rare)

All of these consist of 10 digits beginning with 04 so you could remove spaces and check for all digits, starting digits and length but it gets more compliated with international format phone numbers. This adds these cases:

  • 61 411 234 345
  • +61 411 234 345
  • +61 (0)411 234 345
  • etc

You could include 00 at the front but that could only be used in certain countries that use 00 as an international dialling prefix. I believe the UK is one. So the tricky parts are:

  • Country code is optinal
  • Country code may be prefixed with a +
  • If the country code is present then the leading 0 may be dropped or replaced with (0)
  • International dialling prefix may be used
  • Spacing of digit groups is inconsistent
  • In rare cases hyphens may be used instead of spaces

What I would suggestL

  1. Strip leading 00 if there is one;
  2. Strip leading + if there is one;
  3. Strip leading 61 if it exists;
  4. Replace leading (0) with 0;
  5. Add leading 0 if there isn't one already.
  6. Remove all hyphens and spaces.

If you're not left with 10 digits starting with 04 reject it. Otherwise format it in one of these two formats:

  • Domestic: 0412 345 789
  • International: +61 (0)412 345 789
cletus