'Egypt\'s telephone numbers', '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', ); }#end function phone_eg_metadata; function valid_eg_phone_number($phonenumber) { /* accepts: properly formatted: [+20][ ][(]#[#][)][ ]1234[ ]567[#] common: [(][0#[#]][)][ ]123[ ]456[#] area code could be within paranthises and|or prefixed with 0 */ $regex = "/^ # the same as the model regex above, but with elements made optional (\+20)?\s? # country code, with following space being optional ( (\(0?2\)|0?2)\s?(2|3)\d{3}\s?\d{4} # Greater Cairo, with leading optional zeros and optional braces around the area code | (\(0?" . AREA_CODE_REGEX . "\)|0?" . AREA_CODE_REGEX . ")\s?\d{3}\s?\d{4} # other areas, with optional leading zeros and braces | (\(0?" . MOBILE_CODE_REGEX . "\)|0?" . MOBILE_CODE_REGEX . ")\s?\d{3}\s?\d{4} # mobiles, with optional leading zeros and braces ) (\s?\#\s?\d+)? # extension $/x"; return (bool) preg_match($regex, $phonenumber); }#end function valid_eg_phone_number; function format_eg_phone_number($phonenumber, $field) { if (preg_match("/^" . TELEPHONE_REGEX . "$/x", $phonenumber)) { //already in proper format return $phonenumber; } else { //remove country code, zeros, and braces $phonenumber = preg_replace("/(^(\+20)?\s?|\(0?|\)|^0?)/", '', $phonenumber); } //If there are some spaces in the number assume some level of preformatting if (preg_match("/ /", $phonenumber)) { $regex = "/^ ( (\d{1,2}) # area code \s* # ignore required separator (\d{3,4}) # 4 digits in case of Greater Cairo \s* (\d{4}) ) ((\s*\#)?(\d*))? # extension $/x"; preg_match($regex, $phonenumber, $matches); $area = $matches[2]; $number = $matches[3] . ' ' . $matches[4]; $extension = $matches[7]; } else { //no spaces?, then apply some guessing $regex = "/^ # order is important in this one ( (\d)(\d{4})(\d{4}) # 2 area code, followed by 8 digits begining with 2 or 3: Greater Cairo | (\d{1,2})(\d{3})(\d{4}) # 1 or 2-digit area code followed by 7 digits: regional or mobile ) ((\s*\#)?(\d*))? $/x"; preg_match($regex, $phonenumber, $matches); $area = $matches[2]; $number = $matches[3] . ' ' . $matches[4]; $extension = $matches[5]; } return '+20 (' . $area . ') ' . $number . (empty($extension) ? '' : " #$extension"); }#end function format_eg_phone_number;