phone.eg.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /* 2009/10/24, Ahmad Gharbeia: initial release
  3. General phone number format is +20 (#[#]) #######[#]
  4. currently (10/2009) only Greater Cairo (including all of Giza) has 8 digits:
  5. Regional area codes:
  6. ??????? ?????? 2
  7. ?????????? 3
  8. ????????? 13
  9. ?????? ?? ????? 15
  10. ??????? 4
  11. ??????? 45
  12. ????? 46
  13. ???????? 47
  14. ???????? 48
  15. ???????? 5
  16. ??????? 55
  17. ????? 57
  18. ?????? 62
  19. ??????????? 64
  20. ??????? 65
  21. ??????? 66
  22. ?????? 68
  23. ???? ????? 69
  24. ??? ???? 82
  25. ?????? 84
  26. ?????? 86
  27. ????? 88
  28. ?????? ?????? 92
  29. ????? 93
  30. ?????? 95
  31. ??? 96
  32. ????? 97
  33. mobile network operators' area codes:
  34. Mobinil: 12, 16
  35. Vodafone: 10, 19
  36. Etisalat: 11, 18
  37. **/
  38. define('AREA_CODE_REGEX', "
  39. (
  40. 1(3|5)
  41. |3
  42. |4(5|6|7|8|)
  43. |5(5|7|)
  44. |6(2|4|5|6|8|9)
  45. |8(2|4|6|8)
  46. |9(2|3|5|6|7)
  47. )
  48. ");
  49. define('MOBILE_CODE_REGEX', "1(0|2|1|6|8|9)"); //area codes 10, 11, 12, 16, 18, or 19
  50. define('TELEPHONE_REGEX', "
  51. (\+20)\s # optional countrycode optionally followed by a space
  52. (
  53. \(2\)\s(2|3)\d{3}\s\d{4} # 8-digit numbers begining with 2 or 3 (Greater Cairo: Cairo and Giza, both having area code = 2)
  54. |
  55. \(" . AREA_CODE_REGEX . "\)\s\d{3}\s\d{4} # all other areas' numbers have 7 digits. Checks for correct area codes
  56. |
  57. \(" . MOBILE_CODE_REGEX . "\)\s\d{3}\s\d{4} # mobile operators' networks' area codes; followed by 7 digits
  58. )
  59. (\s\#\s\d+)? # optional extension number shown with a hash divider
  60. ");
  61. function phone_eg_metadata() {
  62. // These strings are translated using t() on output.
  63. return array(
  64. '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',
  65. );
  66. }#end function phone_eg_metadata;
  67. function valid_eg_phone_number($phonenumber) {
  68. /*
  69. accepts:
  70. properly formatted: [+20][ ][(]#[#][)][ ]1234[ ]567[#]
  71. common: [(][0#[#]][)][ ]123[ ]456[#]
  72. area code could be within paranthises and|or prefixed with 0
  73. */
  74. $regex = "/^ # the same as the model regex above, but with elements made optional
  75. (\+20)?\s? # country code, with following space being optional
  76. (
  77. (\(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
  78. |
  79. (\(0?" . AREA_CODE_REGEX . "\)|0?" . AREA_CODE_REGEX . ")\s?\d{3}\s?\d{4} # other areas, with optional leading zeros and braces
  80. |
  81. (\(0?" . MOBILE_CODE_REGEX . "\)|0?" . MOBILE_CODE_REGEX . ")\s?\d{3}\s?\d{4} # mobiles, with optional leading zeros and braces
  82. )
  83. (\s?\#\s?\d+)? # extension
  84. $/x";
  85. return (bool) preg_match($regex, $phonenumber);
  86. }#end function valid_eg_phone_number;
  87. function format_eg_phone_number($phonenumber, $field) {
  88. if (preg_match("/^" . TELEPHONE_REGEX . "$/x", $phonenumber)) { //already in proper format
  89. return $phonenumber;
  90. }
  91. else { //remove country code, zeros, and braces
  92. $phonenumber = preg_replace("/(^(\+20)?\s?|\(0?|\)|^0?)/", '', $phonenumber);
  93. }
  94. //If there are some spaces in the number assume some level of preformatting
  95. if (preg_match("/ /", $phonenumber)) {
  96. $regex = "/^
  97. (
  98. (\d{1,2}) # area code
  99. \s* # ignore required separator
  100. (\d{3,4}) # 4 digits in case of Greater Cairo
  101. \s*
  102. (\d{4})
  103. )
  104. ((\s*\#)?(\d*))? # extension
  105. $/x";
  106. preg_match($regex, $phonenumber, $matches);
  107. $area = $matches[2];
  108. $number = $matches[3] . ' ' . $matches[4];
  109. $extension = $matches[7];
  110. }
  111. else { //no spaces?, then apply some guessing
  112. $regex = "/^ # order is important in this one
  113. (
  114. (\d)(\d{4})(\d{4}) # 2 area code, followed by 8 digits begining with 2 or 3: Greater Cairo
  115. |
  116. (\d{1,2})(\d{3})(\d{4}) # 1 or 2-digit area code followed by 7 digits: regional or mobile
  117. )
  118. ((\s*\#)?(\d*))?
  119. $/x";
  120. preg_match($regex, $phonenumber, $matches);
  121. $area = $matches[2];
  122. $number = $matches[3] . ' ' . $matches[4];
  123. $extension = $matches[5];
  124. }
  125. return '+20 (' . $area . ') ' . $number . (empty($extension) ? '' : " #$extension");
  126. }#end function format_eg_phone_number;