phone.ph.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Philippine phone numbers.
  5. */
  6. function phone_ph_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Philippine phone number<br />Example of valid Philippine phone numbers: +63 (2) 123-4567 or +63 (2) 123-4567 loc. 123 or mobile +63 (919) 123-4567',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is a valid ten-digit Philippine phone number
  14. *
  15. * @param string $phonenumber
  16. * @return boolean Returns boolean FALSE if the phone number is not valid.
  17. */
  18. function valid_ph_phone_number($phonenumber) {
  19. /*
  20. Accepts:
  21. +63197071234567
  22. +63197071234567
  23. +63(19707) 1234567
  24. +63(19707) 123-4567
  25. +63 19707 123 4567
  26. +63 19707 123-4567
  27. (19707) 1234567 loc. 1234
  28. */
  29. $regex = "/
  30. (
  31. (^\+63\s?\(?\d{5}\)?|^\(?\d{5}\)?){1}\s?\d{3}(\S?|\s?)?\d{4} # 5 digit area code with optional +63 internationalisation or not, optional spaces and brackets.
  32. |
  33. (^\+63\s?\(?\d{4}\)?|^\(?\d{4}\)?){1}\s?\d{3}(\S?|\s?)?\d{4} # 4 digit area code with optional +63 internationalisation or not, optional spaces and brackets.
  34. |
  35. (^\+63\s?\(?\d{3}\)?|^\(?\d{3}\)?){1}\s?\d{3}(\S?|\s?)?\d{4} # 3 digit area code with optional +63 internationalisation or not, optional spaces and brackets.
  36. |
  37. (^\+63\s?\(?\d{2}\)?|^\(?\d{2}\)?){1}\s?\d{3}(\S?|\s?)?\d{4} # 2 digit area code with optional +63 internationalisation or not, optional spaces and brackets.
  38. |
  39. (^\+63\s?\(?\d{1}\)?|^\(?\d{1}\)?){1}\s?\d{3}(\S?|\s?)?\d{4} # 1 digit area code with optional +63 internationalisation or not, optional spaces and brackets.
  40. )
  41. (\s?\#\d*)? # optional extension number shown with a loc. divider
  42. /x";
  43. // return true if valid, false otherwise
  44. if (!preg_match($regex, $phonenumber)) {
  45. return FALSE;
  46. }
  47. else
  48. {
  49. return TRUE;
  50. }
  51. }
  52. /**
  53. * Convert a valid Philippine phone number into standard +63 (2) 123-4567 or +63 (2) 123-4567 loc. 123 or mobile +63 (919) 123-4567
  54. *
  55. * @param $phonenumber must be a valid ten-digit number (with optional extension)
  56. *
  57. */
  58. function format_ph_phone_number($phonenumber, $field = FALSE) {
  59. $area = $number = $extension = $description = '';
  60. //Simplify to 10 digit number and clean up ready for international reformat.
  61. $phonenumber = preg_replace("/^\+63/","",$phonenumber);
  62. $phonenumber = preg_replace("/\(/","",$phonenumber);
  63. $phonenumber = preg_replace("/\)/","",$phonenumber);
  64. //If there are some spaces in the number assume some level of preformatting
  65. $regex = "/
  66. # 5 digit area code.
  67. (
  68. (\d{5}) # capture 5 digit area code
  69. (\s*)? # ignore required separator to make a distinction with other area codes
  70. (\d{3}) # capture first set of numbers in the local number
  71. (\S?|\s*)? # ignore optional separator
  72. (\d{4}) # capture second set of numbers in the local number
  73. |
  74. # 4 digit area code.
  75. (\d{4}) # capture 4 digit area code
  76. (\s*)? # ignore required seperator
  77. (\d{3}) # capture first set of numbers in the local number
  78. (\S?|\s*)? # ignore possible boundary
  79. (\d{4}) # capture second set of numbers in the local number
  80. |
  81. # 3 digit area code.
  82. (\d{3}) # capture 3 digit area code
  83. (\s*)? # ignore required seperator
  84. (\d{3}) # capture first set of numbers in the local number
  85. (\S?|\s*)? # ignore possible boundary
  86. (\d{4}) # capture second set of numbers in the local number
  87. |
  88. # 2 digit area code.
  89. (\d{2}) # capture 2 digit area code
  90. (\s*)? # ignore required seperator
  91. (\d{3}) # capture first set of numbers in the local number
  92. (\S?|\s*)? # ignore possible boundary
  93. (\d{4}) # capture second set of numbers in the local number
  94. |
  95. # 1 digit area code.
  96. (\d{1}) # capture 1 digit area code
  97. (\s*)? # ignore required boundary to make a distinction with other area codes
  98. (\d{3}) # capture first set of numbers in the local number
  99. (\S?|\s*)? # ignore possible boundary
  100. (\d{4}) # capture second set of numbers in the local number
  101. )
  102. # capture the optional extension number
  103. (\s*loc\.\s*|\s*ext\.\s*)?
  104. (\d*)?
  105. ([a-zA-Z0-9\-\. \/\,\']{0,})?
  106. /x";
  107. preg_match($regex, $phonenumber, $matches);
  108. $area = $matches[2] . $matches[7] . $matches[12] . $matches[17] . $matches[22];
  109. $number = $matches[4] . $matches[9] . $matches[14] . $matches[19] . $matches[24] . '-' . $matches[6] . $matches[11] . $matches[16] . $matches[21] . $matches[26];
  110. $extension = $matches[28];
  111. $description = $matches[29];
  112. $phonenumber = '+63 (' . $area . ') ' . $number;
  113. $phonenumber .= (empty($extension)) ? '' : " loc. $extension";
  114. $phonenumber .= (empty($description))? '' : " $description";
  115. return $phonenumber;
  116. }