1

I got an excel list with certain amount of phone numbers (US/Canada/EU/Asia), since the phone column is in the untidy format, I need to parse the number to become printable format like:

(123)456-7890

Here are few phone format that might listed in the column: (XX can be any country code) - +XX.98765-4321 - XX.(987) 654-3210 - 1.(987) 654-3210 - +1.(987) 654-3210 - 1.987.654.3210 (1234) (1234) (1234) - 1.987.654.3210 (1234) (1234) - 1.987.654.3210 (1234) - 1.987.654.3210 (123) - 1.987.654.3210 (12345) - 9876543210 #1234

As example phone format above, the number with prefix +81, 45, 1, +1 will be get rid, where the last number within parentheses/bracket is an extension that need to be extract.

function reformat($phone){
    $find = array('1.','+1');
    $phone = str_replace($find,'',$phone);

    $phone = preg_replace('/[^0-9]*/','',$phone);

    $sArea = substr($phone,0,3);
    $sPrefix = substr($phone,3,3);
    $sNumber = substr($phone,6,4);

    if(strlen($phone) > 10) $sExt = substr($phone, -4);

    $phone = '('.$sArea.') '.$sPrefix.'-'.$sNumber;
    $ext = ($sExt) ? (int)$sExt : null;

    $data = array($phone, $ext);

    return($data);
}

the function above can parsed the number with output such as: 1.987.654.3210 (1234) (1234) (1234) -> (987)654-3210 and extension 1234

If the number come with prefix +XX, +XX. or XX. and extension #1234 or (12345), the output will not accurate.

Is that any addon to the function so that it could formatted welly from the above possible input?

The Grimmer
  • 47
  • 1
  • 6
  • @jay-blanchard I don't think this question qualifies as duplicate for the marked one. If you read the other question (and answer), these refer specifically to UK numbers, while this question here asks more broadly how to improve the parsing of this existing list of number from very different countries. – Fitzi Apr 23 '19 at 10:13
  • Take look at https://stackoverflow.com/a/54417324/9623432 it should help you – Kristijan Mikulec Apr 23 '19 at 11:05
  • There are several duplicates @Fitzi. The OP should search them and will likely find one they need. – Jay Blanchard Apr 23 '19 at 12:08

0 Answers0