View source
<?php
define('AREA_CODE_REGEX', "\n\t\t(\n\t\t\t 1(3|5)\n\t\t\t|3\n\t\t\t|4(5|6|7|8|)\n\t\t\t|5(5|7|)\n\t\t\t|6(2|4|5|6|8|9)\n\t\t\t|8(2|4|6|8)\n\t\t\t|9(2|3|5|6|7)\n\t\t)\n");
define('MOBILE_CODE_REGEX', "1(0|2|1|6|8|9)");
define('TELEPHONE_REGEX', "\n\t(\\+20)\\s\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t# optional countrycode optionally followed by a space\n\t\t(\n\t\t\t\t\\(2\\)\\s(2|3)\\d{3}\\s\\d{4}\t\t\t\t\t\t\t\t# 8-digit numbers begining with 2 or 3 (Greater Cairo: Cairo and Giza, both having area code = 2)\n\t\t\t|\n\t\t\t\t\\(" . AREA_CODE_REGEX . "\\)\\s\\d{3}\\s\\d{4}\t\t# all other areas' numbers have 7 digits. Checks for correct area codes\n\t\t\t|\n\t\t\t\t\\(" . MOBILE_CODE_REGEX . "\\)\\s\\d{3}\\s\\d{4}\t\t# mobile operators' networks' area codes; followed by 7 digits\n\t\t)\n\t\t(\\s\\#\\s\\d+)?\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t# optional extension number shown with a hash divider\n");
function phone_eg_metadata() {
return array(
'error' => '"%value" is not a valid phone number in Egypt. Telephone numbers in Egypt have the format +20 (#[#]) 1234567[8], where the digts between ( ) is the area or network code, without a leading zero; and digits between [ ] are optional',
);
}
function valid_eg_phone_number($phonenumber) {
$regex = "/^\t\t\t\t\t\t# the same as the model regex above, but with elements made optional\n\t\t(\\+20)?\\s?\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t# country code, with following space being optional\n (\n\t\t\t\t(\\(0?2\\)|0?2)\\s?(2|3)\\d{3}\\s?\\d{4}\t\t\t\t# Greater Cairo, with leading optional zeros and optional braces around the area code\n\t\t\t|\n\t\t\t\t(\\(0?" . AREA_CODE_REGEX . "\\)|0?" . AREA_CODE_REGEX . ")\\s?\\d{3}\\s?\\d{4}\t\t\t\t\t# other areas, with optional leading zeros and braces\n\t\t\t|\n\t\t\t\t(\\(0?" . MOBILE_CODE_REGEX . "\\)|0?" . MOBILE_CODE_REGEX . ")\\s?\\d{3}\\s?\\d{4}\t\t# mobiles, with optional leading zeros and braces\n\t\t)\n\t\t(\\s?\\#\\s?\\d+)? \t\t\t\t\t\t\t\t\t# extension\n\t\$/x";
return (bool) preg_match($regex, $phonenumber);
}
function format_eg_phone_number($phonenumber, $field) {
if (preg_match("/^" . TELEPHONE_REGEX . "\$/x", $phonenumber)) {
return $phonenumber;
}
else {
$phonenumber = preg_replace("/(^(\\+20)?\\s?|\\(0?|\\)|^0?)/", '', $phonenumber);
}
if (preg_match("/ /", $phonenumber)) {
$regex = "/^\n \t(\n\t\t\t\t(\\d{1,2})\t\t\t\t\t# area code\n\t\t\t\t\\s* \t\t\t\t\t\t # ignore required separator\n\t\t\t\t(\\d{3,4})\t\t\t\t\t# 4 digits in case of Greater Cairo\n\t\t\t\t\\s*\n\t\t\t\t(\\d{4})\n\t\t\t)\n\t\t\t((\\s*\\#)?(\\d*))? \t\t\t\t\t\t\t\t\t# extension\n\t\t\$/x";
preg_match($regex, $phonenumber, $matches);
$area = $matches[2];
$number = $matches[3] . ' ' . $matches[4];
$extension = $matches[7];
}
else {
$regex = "/^ # order is important in this one\n\t\t\t(\n\t\t\t\t\t(\\d)(\\d{4})(\\d{4})\t\t\t\t# 2 area code, followed by 8 digits begining with 2 or 3: Greater Cairo\n\t\t\t\t|\n\t\t\t\t\t(\\d{1,2})(\\d{3})(\\d{4})\t\t\t\t# 1 or 2-digit area code followed by 7 digits: regional or mobile\n\t\t\t)\n\t\t\t((\\s*\\#)?(\\d*))?\n\t\t\$/x";
preg_match($regex, $phonenumber, $matches);
$area = $matches[2];
$number = $matches[3] . ' ' . $matches[4];
$extension = $matches[5];
}
return '+20 (' . $area . ') ' . $number . (empty($extension) ? '' : " #{$extension}");
}