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?