Created
May 9, 2012 05:33
-
-
Save tsnoad/2642139 to your computer and use it in GitHub Desktop.
simple checking and correction of australian and international phone numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
function checkPhone($int_dial_code, $area_code, $local_number) { | |
//try to check and reformat australian numbers | |
if ($int_dial_code == "61") { | |
//combine the area code and local number | |
$full_phone = $area_code.$local_number; | |
//strip spaces and hyphens | |
$full_phone = str_replace(array(" ", "-"), "", $full_phone); | |
//australian numbers in the international format: | |
// might start with either 0011, 00, or 0 | |
// then might have + | |
// then have 61 | |
// then have either 1 to 9, 01 to 09, (0)1 to (0)9, (01) to (09), or -1 to -9 | |
$aus_int_prefix = "(0011|00|0)?\+?61([1-9]|0[1-9]|\(0\)[1-9]|\(0[1-9]\)|\-[1-9])"; | |
//australian numbers in the domestic format start with either: | |
// start with 01 to 09, (0)1 to (0)9, or (01) to (09) | |
$aus_dom_prefix = "(0[1-9]|\(0\)[1-9]|\(0[1-9]\))"; | |
//numbers in the international format have the international prefix, then 8 digits | |
if (preg_match("/^".$aus_int_prefix."([0-9]{8,8})$/", $full_phone, $matches)) { | |
//extract the area code from the internationally formatted number | |
$val_area_code = "0".$matches[2]; | |
//extract the local number from the internationally formatted number | |
$val_local_number = $matches[3]; | |
//numbers in the domestic format have the domestic prefix, then 8 digits | |
} else if (preg_match("/^".$aus_dom_prefix."([0-9]{8,8})$/", $full_phone, $matches)) { | |
//extract the area code from the full number | |
$val_area_code = $matches[1]; | |
//extract the local number from the full number | |
$val_local_number = $matches[2]; | |
//1300 numbers start with 1300 then have 6 digits | |
} else if (preg_match("/^(1300)([0-9]{6,6})$/", $full_phone, $matches)) { | |
//set the area code as 1300 | |
$val_area_code = $matches[1]; | |
//set the local number as the remaining 6 digits | |
$val_local_number = $matches[2]; | |
//1800 numbers start with 1800 then have 6 digits | |
} else if (preg_match("/^(1800)([0-9]{6,6})$/", $full_phone, $matches)) { | |
//set the area code as 1300 | |
$val_area_code = $matches[1]; | |
//set the local number as the remaining 6 digits | |
$val_local_number = $matches[2]; | |
//13 numbers start with 13 then have 4 digits | |
} else if (preg_match("/^(13)([0-9]{4,4})$/", $full_phone, $matches)) { | |
//set the area code as 13 | |
$val_area_code = $matches[1]; | |
//set the local number as the remaining 4 digits | |
$val_local_number = $matches[2]; | |
//pagers and old analog phone start with 014 to 018 then have 6 digits | |
} else if (preg_match("/^(01[45678])([0-9]{6,6})$/", $full_phone, $matches)) { | |
//set the area code as the 014 to 018 portion | |
$val_area_code = $matches[1]; | |
//set the local number as the remaining 6 digits | |
$val_local_number = $matches[2]; | |
} else { | |
throw new Exception("This is not a valid australian phone number\n"); | |
} | |
//try to check non-australian numbers | |
} else { | |
//combine the area code and local number | |
$full_phone = $area_code.$local_number; | |
//non-australian numbers must contain 5 digits | |
if (!preg_match("/^.*[0-9].*[0-9].*[0-9].*[0-9].*[0-9].*$/", $full_phone, $matches)) { | |
throw new Exception("This is not a valid non-australian phone number\n"); | |
} | |
//if there is no area code... | |
if (strlen($area_code) < 1) { | |
//... steal the first digit an use it as area code | |
$val_area_code = substr($full_phone, 0, 1); | |
$val_local_number = substr($full_phone, 1); | |
//no reformatting required | |
} else { | |
$val_area_code = $area_code; | |
$val_local_number = $local_number; | |
} | |
} | |
//return the checked, reformatted number | |
return array($val_area_code, $val_local_number); | |
} | |
function test() { | |
//check that perfect australian numbers are handled correctly | |
if (checkPhone("61", "02", "62701234") != array("02", "62701234")) throw New Exception("should have succeded"); | |
if (checkPhone("61", "1800", "123456") != array("1800", "123456")) throw New Exception("should have succeded"); | |
if (checkPhone("61", "13", "1234") != array("13", "1234")) throw New Exception("should have succeded"); | |
//check that imperfect australian numbers are handled correctly | |
if (checkPhone("61", "", "0262701234") != array("02", "62701234")) throw New Exception("should have succeded"); | |
if (checkPhone("61", "18", "00123456") != array("1800", "123456")) throw New Exception("should have succeded"); | |
if (checkPhone("61", "131", "234") != array("13", "1234")) throw New Exception("should have succeded"); | |
//check that perfect international numbers are handled correctly | |
if (checkPhone("1", "123", "1231234") != array("123", "1231234")) throw New Exception("should have succeded"); | |
//check that imperfect international numbers are handled correctly | |
if (checkPhone("1", "", "1231231234") != array("1", "231231234")) throw New Exception("should have succeded"); | |
//check that unreadable australian numbers are handled correctly | |
try { | |
checkPhone("61", "123", "1234"); | |
} catch (Exception $e) { | |
$caught = true; | |
} | |
if (!$caught) throw New Exception("should have failed"); | |
unset($caught); | |
//check that unreadable australian numbers are handled correctly | |
try { | |
checkPhone("61", "foo", "bar"); | |
} catch (Exception $e) { | |
$caught = true; | |
} | |
if (!$caught) throw New Exception("should have failed"); | |
unset($caught); | |
//check that unreadable international numbers are handled correctly | |
try { | |
checkPhone("61", "", "123"); | |
} catch (Exception $e) { | |
$caught = true; | |
} | |
if (!$caught) throw New Exception("should have failed"); | |
unset($caught); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to check UK telephone numbers this in PHP may be useful for starters... https://raw.github.com/g1smd/Drupal-CCK-Phone-GB/ea97970a73d3b2ff05cd9fc4a3bac7a43d2fa945/phone.gb.inc