phone.nz.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for New Zealand phone numbers.
  5. */
  6. function phone_nz_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid New Zealand phone number!<br>New Zealand phone numbers should contain only numbers, spaces, brackets and "-". They should look like 04 123 4567 or can optionally omit the leading 0 and have a prefix of "+64".',
  10. );
  11. }
  12. /**
  13. * Verification for New Zealand Phone Numbers.
  14. * As supplied by http://www.itu.int/itudoc/itu-t/number/n/nzl/76195_ww9.doc, April 2009
  15. *
  16. * @param string $phonenumber
  17. * @return boolean returns boolean FALSE if the phone number is not valid.
  18. */
  19. function valid_nz_phone_number($phonenumber) {
  20. //$phonenumber = trim($phonenumber);
  21. // strip formatting chars
  22. $phonenumber = preg_replace('/[\-() ]/', '', $phonenumber);
  23. // strip optional '+64' or '0' prefixes
  24. $phonenumber = preg_replace('/^(\+64|0)/', '', $phonenumber);
  25. //$rules[] = array("Prefix","Minimum length","Maximum length");
  26. // special purpose service codes and numbers
  27. /*// Enable if required
  28. $rules[] = array('10', 3, 3);
  29. $rules[] = array('12', 6, 7); // NZ Direct Service
  30. $rules[] = array('60', 4, 4); // Directory Assistance (operator only) - Maybe shouldn't be in here
  31. $rules[] = array('83', 5, 8); // "Enhanced Voice Services"
  32. $rules[] = array('86', 8, 8); // "Enhanced Paging Services"
  33. $rules[] = array('87', 4, 8); // "PSTN access to data services"
  34. */
  35. // Mobile
  36. $rules[] = array('20', 9, 9); // mobile radio / Orcon mobile
  37. $rules[] = array('22', 8, 10); // NZ Communications (actual lengths subject to change)
  38. $rules[] = array('210', 8, 10); // Vodafone
  39. $rules[] = array('211', 8, 9); // Vodafone
  40. $rules[] = array('212', 8, 9); // Vodafone
  41. $rules[] = array('213', 8, 9); // Vodafone
  42. $rules[] = array('214', 8, 9); // Vodafone
  43. $rules[] = array('215', 8, 9); // Vodafone
  44. $rules[] = array('216', 8, 9); // Vodafone
  45. $rules[] = array('217', 8, 9); // Vodafone
  46. $rules[] = array('218', 8, 9); // Vodafone
  47. $rules[] = array('219', 8, 9); // Vodafone
  48. $rules[] = array('24', 8, 8); // Scott Base
  49. //$rules[] = array('25', 8, 9); // Old Telecom 025, now unused
  50. $rules[] = array('26', 8, 9); // Telecom Paging
  51. $rules[] = array('27', 9, 9); // Telecom 027 mobile
  52. $rules[] = array('29', 8, 9); // TelstraClear mobile
  53. // South Island regional
  54. $rules[] = array('32', 8, 8);
  55. $rules[] = array('33', 8, 8);
  56. $rules[] = array('34', 8, 8);
  57. $rules[] = array('35', 8, 8);
  58. $rules[] = array('36', 8, 8);
  59. $rules[] = array('37', 8, 8);
  60. $rules[] = array('39', 8, 8);
  61. // Wellington regional
  62. $rules[] = array('42', 8, 8);
  63. $rules[] = array('43', 8, 8);
  64. $rules[] = array('44', 8, 8);
  65. $rules[] = array('45', 8, 8);
  66. $rules[] = array('46', 8, 8);
  67. $rules[] = array('48', 8, 8);
  68. $rules[] = array('49', 8, 8);
  69. // Manawatu, Taranaki, Hawkes Bay, Gisborne, Wairarapa, Otaki regional
  70. $rules[] = array('62', 8, 8);
  71. $rules[] = array('63', 8, 8);
  72. $rules[] = array('67', 8, 8);
  73. $rules[] = array('68', 8, 8);
  74. $rules[] = array('69', 8, 8);
  75. // Waikato, BOP, Taumarunui regional
  76. $rules[] = array('73', 8, 8);
  77. $rules[] = array('75', 8, 8);
  78. $rules[] = array('62', 8, 8);
  79. $rules[] = array('78', 8, 8);
  80. $rules[] = array('79', 8, 8);
  81. // Freecall
  82. $rules[] = array('80', 8, 10);
  83. // Pay-call
  84. $rules[] = array('90', 8, 10);
  85. // Auckland + Northland regional
  86. $rules[] = array('92', 8, 8);
  87. $rules[] = array('93', 8, 8);
  88. $rules[] = array('94', 8, 8);
  89. $rules[] = array('95', 8, 8);
  90. $rules[] = array('96', 8, 8);
  91. $rules[] = array('98', 8, 8);
  92. $rules[] = array('99', 8, 8);
  93. foreach ($rules as $rule) {
  94. if (preg_match('/^'.$rule[0].'/', $phonenumber) && strlen($phonenumber) >= $rule[1] && strlen($phonenumber) <= $rule[2]) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * Formatting for New Zealand Phone Numbers.
  102. *
  103. * @param string $phonenumber
  104. * @return string Returns a string containing the phone number with some formatting.
  105. */
  106. function format_nz_phone_number($phonenumber, $field) {
  107. $prefix = '';
  108. $extension = '';
  109. // strip old formatting chars
  110. $phonenumber = preg_replace('/[\-() ]/', '', $phonenumber);
  111. /*
  112. * strip and save the +64 prefix if found
  113. */
  114. if (preg_match('/^\+64/', $phonenumber, $match)) {
  115. $prefix = '+64';
  116. $phonenumber = str_replace('+64', '', $phonenumber);
  117. }
  118. else {
  119. $prefix = '0';
  120. /*
  121. * Remove a leading 0, if present
  122. */
  123. if (preg_match('/^0/', $phonenumber, $match)) {
  124. $phonenumber = substr($phonenumber, 1);
  125. }
  126. }
  127. /*
  128. * strip and save the extension (x9999) postfix if found
  129. */
  130. if (preg_match('/(x[0-9]+)$/', $phonenumber, $match)) {
  131. $extension = ' '.$match[1];
  132. $phonenumber = preg_replace('/x[0-9]+$/', '', $phonenumber);
  133. }
  134. /*
  135. * geographic numbers
  136. * Eg. (04) 123 4578
  137. */
  138. if (preg_match('/^([34679])([0-9]{3})([0-9]{4})$/', $phonenumber, $match)) {
  139. return _format_nz_phone_number_prefix($prefix, $match[1]) . $match[2] . ' ' . $match[3] . $extension;
  140. }
  141. /*
  142. * 8 digit mobile numbers
  143. * Eg. 021 123 123
  144. */
  145. if (preg_match('/^(2[12679])([0-9]{3})([0-9]{3})$/', $phonenumber, $match)) {
  146. return _format_nz_phone_number_prefix($prefix, $match[1]) . $match[2] . ' ' . $match[3] . $extension;
  147. }
  148. /*
  149. * 9 digit mobile numbers
  150. * Eg. 021 123 4567
  151. */
  152. if (preg_match('/^(2[12679])([0-9]{3})([0-9]{4})$/', $phonenumber, $match)) {
  153. return _format_nz_phone_number_prefix($prefix, $match[1]) . $match[2] . ' ' . $match[3] . $extension;
  154. }
  155. /*
  156. * 10 digit mobile numbers
  157. * Eg. 021 1234 1234
  158. */
  159. if (preg_match('/^(2[12679])([0-9]{4})([0-9]{4})$/', $phonenumber, $match)) {
  160. return _format_nz_phone_number_prefix($prefix, $match[1]) . $match[2] . ' ' . $match[3] . $extension;
  161. }
  162. /**
  163. * 0800/0900 numbers (a bit random)
  164. */
  165. if (preg_match('/^(900|800)(\d+)$/', $phonenumber, $match)) {
  166. // How we format depends on the length
  167. $formatted = null;
  168. switch(strlen($match[2])) {
  169. case 5: $formatted = preg_replace('/^(\d{2})(\d{3})$/', '$1$2', $match[2]); break;
  170. case 6: $formatted = preg_replace('/^(\d{3})(\d{3})$/', '$1$2', $match[2]); break;
  171. case 7: $formatted = preg_replace('/^(\d{3})(\d{4})$/', '$1 $2', $match[2]); break;
  172. }
  173. return _format_nz_phone_number_prefix($prefix, $match[1]).$formatted;
  174. }
  175. // default (probably bad)
  176. return $prefix . $phonenumber . $extension;
  177. }
  178. /**
  179. * Formats the prefix as either +64 4 or (04), depending on the original prefix context
  180. *
  181. * @param string $prefix either '+64' or '0'
  182. * @param string $area_code the area code, eg. 4, 21, 27 etc
  183. * @return string the formatted prefix
  184. */
  185. function _format_nz_phone_number_prefix($prefix, $area_code) {
  186. $area_code = intval($area_code);
  187. if (in_array($area_code, array('800', '900'))) {
  188. return ($prefix == '0') ? $prefix.$area_code.' ' : $prefix.' '.$area_code.' ';
  189. }
  190. else {
  191. return ($prefix == '0') ? '('.$prefix.$area_code.') ' : $prefix.' '.$area_code.' ';
  192. }
  193. }