ondrobaco
ondrobaco

Reputation: 737

australian mobile numbers strings formatting

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?

Upvotes: 1

Views: 4116

Answers (6)

Pete855217
Pete855217

Reputation: 1602

There are much more efficient ways to do this, and the memory useage of the following code is not perfect (modifying incoming parameter), but if you want to follow the logic without using regexes, this will work (note doesn't handle 13 or 1800 numbers, but that's easily fixed if you need these too):

  function fmtAusPhoneNum($phoneNum) {
    // First strip spaces and non numerics
    $phoneNum = preg_replace('/\D+/', '', $phoneNum);
    // Strip AUS international access code
    if (substr($phoneNum, 0, 4) == '0011') {
        $phoneNum = substr($phoneNum, 4, strlen($phoneNum) - 4);
    }
    // Strip UK (and some others) international code
    if (substr($phoneNum, 0, 2) == '00') {
        $phoneNum = substr($phoneNum, 2, strlen($phoneNum) - 2);
    }
    // Add any other international prefixes here.

    // If first 2 digits are '61' then international format, take off 2
    if (substr($phoneNum, 0, 2) == '61') {
        $phoneNum = substr($phoneNum, 2, strlen($phoneNum) - 2);
    }
    // If STD 0 code, take it off.
    elseif (substr($phoneNum, 0, 1) == '0') {
        $phoneNum = substr($phoneNum, 1, strlen($phoneNum) - 1);
    }
    // Now should have a 9 char long digit.
    if (strlen($phoneNum)!=9) {
        echo "Warning: can't parse Australian number - less access codes, should have 9 digits only\n";
    }
    // If first digit is 4, it's a mobile, format 04xx xxx xxx
    if (substr($phoneNum, 0, 1) == '4') {
        $phoneNum = '0' . substr($phoneNum, 0, 3) . ' ' . substr($phoneNum, 3, 3) . ' ' . substr($phoneNum, 6, 3);
    } else {
        // It's a landline number, split into area code eg 03 and 2 lots of 4 digits
        $phoneNum = '0' . substr($phoneNum, 0, 1) . ' ' . substr($phoneNum, 1, 4) . ' ' . substr($phoneNum, 5, 4);
    }
    return $phoneNum;
}

// Test it
echo fmtAusPhoneNum('0061 421 123 123') . "\n";
echo fmtAusPhoneNum('0421741940') . "\n";
echo fmtAusPhoneNum('+61421741940') . "\n";
echo fmtAusPhoneNum('61394190231') . "\n";
echo fmtAusPhoneNum('03 5521 7475') . "\n";

Upvotes: 1

cletus
cletus

Reputation: 625047

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

Upvotes: 2

SilentGhost
SilentGhost

Reputation: 319561

  • 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, ' ');

Upvotes: 5

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

^(?:\+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.

Upvotes: 0

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

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.

Upvotes: 1

Ewan Todd
Ewan Todd

Reputation: 7312

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

Upvotes: 0

Related Questions