phone.au.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Australian phone numbers.
  5. */
  6. function phone_au_metadata() {
  7. return array(
  8. 'error' => '"%value" is not a valid Australian phone number<br>Australian phone numbers should contain only numbers with an optional prefix of "+61"',
  9. );
  10. }
  11. /**
  12. * Verification for Australian Phone Numbers.
  13. * According to http://www.itu.int/itudoc/itu-t/number/a/aus/70772.html
  14. * (Released 2005/10/03, retrieved 2008/04/14)
  15. *
  16. * @param string $phonenumber
  17. * @return boolean Returns boolean FALSE if the phone number is not valid.
  18. */
  19. function valid_au_phone_number($phonenumber) {
  20. //$phonenumber = trim($phonenumber);
  21. // strip formatting chars
  22. $phonenumber = preg_replace('/[\-() ]/', '', $phonenumber);
  23. // strip optional '+61' or '0' prefixes
  24. $phonenumber = preg_replace('/^(\+61|0)/', '', $phonenumber);
  25. //$rules[] = array("Prefix","Minimum length","Maximum length");
  26. $rules[] = array("10", 5, 15);
  27. $rules[] = array("12", 5, 15);
  28. $rules[] = array("13", 6, 10);
  29. $rules[] = array("1451", 9, 9);
  30. $rules[] = array("1452", 9, 9);
  31. $rules[] = array("1453", 9, 9);
  32. $rules[] = array("1471", 9, 9);
  33. $rules[] = array("16", 5, 9);
  34. $rules[] = array("1800", 10, 10);
  35. $rules[] = array("1801", 8, 8);
  36. $rules[] = array("1900", 10, 10);
  37. $rules[] = array("1901", 10, 10);
  38. $rules[] = array("1902", 10, 10);
  39. $rules[] = array("1906", 10, 10);
  40. $rules[] = array("1983", 9, 9);
  41. $rules[] = array("2001", 4, 4);
  42. $rules[] = array("2002", 4, 4);
  43. $rules[] = array("2003", 4, 4);
  44. //$rules[] = array("2", 9, 9); // More specific rules follow
  45. $rules[] = array("240", 9, 9);
  46. $rules[] = array("242", 9, 9);
  47. $rules[] = array("243", 9, 9);
  48. $rules[] = array("244", 9, 9);
  49. $rules[] = array("245", 9, 9);
  50. $rules[] = array("246", 9, 9);
  51. $rules[] = array("247", 9, 9);
  52. $rules[] = array("248", 9, 9);
  53. $rules[] = array("249", 9, 9);
  54. $rules[] = array("260", 9, 9);
  55. $rules[] = array("261", 9, 9);
  56. $rules[] = array("262", 9, 9);
  57. $rules[] = array("263", 9, 9);
  58. $rules[] = array("264", 9, 9);
  59. $rules[] = array("265", 9, 9);
  60. $rules[] = array("266", 9, 9);
  61. $rules[] = array("267", 9, 9);
  62. $rules[] = array("268", 9, 9);
  63. $rules[] = array("269", 9, 9);
  64. $rules[] = array("28", 9, 9);
  65. $rules[] = array("29", 9, 9);
  66. //$rules[] = array("3", 9, 9); // More specific rules follow
  67. $rules[] = array("350", 9, 9);
  68. $rules[] = array("351", 9, 9);
  69. $rules[] = array("352", 9, 9);
  70. $rules[] = array("353", 9, 9);
  71. $rules[] = array("354", 9, 9);
  72. $rules[] = array("355", 9, 9);
  73. $rules[] = array("356", 9, 9);
  74. $rules[] = array("357", 9, 9);
  75. $rules[] = array("358", 9, 9);
  76. $rules[] = array("359", 9, 9);
  77. $rules[] = array("362", 9, 9);
  78. $rules[] = array("363", 9, 9);
  79. $rules[] = array("364", 9, 9);
  80. $rules[] = array("38", 9, 9);
  81. $rules[] = array("39", 9, 9);
  82. // begin mobile phone numbers
  83. $rules[] = array("400", 9, 9);
  84. $rules[] = array("401", 9, 9);
  85. $rules[] = array("402", 9, 9);
  86. $rules[] = array("403", 9, 9);
  87. $rules[] = array("404", 9, 9);
  88. $rules[] = array("405", 9, 9);
  89. $rules[] = array("406", 9, 9);
  90. $rules[] = array("407", 9, 9);
  91. $rules[] = array("408", 9, 9);
  92. $rules[] = array("409", 9, 9);
  93. $rules[] = array("410", 9, 9);
  94. $rules[] = array("411", 9, 9);
  95. $rules[] = array("412", 9, 9);
  96. $rules[] = array("413", 9, 9);
  97. $rules[] = array("414", 9, 9);
  98. $rules[] = array("415", 9, 9);
  99. $rules[] = array("416", 9, 9);
  100. $rules[] = array("417", 9, 9);
  101. $rules[] = array("418", 9, 9);
  102. $rules[] = array("419", 9, 9);
  103. $rules[] = array("420", 9, 9);
  104. $rules[] = array("4200", 9, 9);
  105. $rules[] = array("42010", 9, 9);
  106. $rules[] = array("421", 9, 9);
  107. $rules[] = array("422", 9, 9);
  108. $rules[] = array("423", 9, 9);
  109. $rules[] = array("424", 9, 9);
  110. $rules[] = array("425", 9, 9);
  111. $rules[] = array("4251", 9, 9);
  112. $rules[] = array("4252", 9, 9);
  113. $rules[] = array("4253", 9, 9);
  114. $rules[] = array("4256", 9, 9);
  115. $rules[] = array("4257", 9, 9);
  116. $rules[] = array("4258", 9, 9);
  117. $rules[] = array("427", 9, 9);
  118. $rules[] = array("428", 9, 9);
  119. $rules[] = array("429", 9, 9);
  120. $rules[] = array("430", 9, 9);
  121. $rules[] = array("4300", 9, 9);
  122. $rules[] = array("4301", 9, 9);
  123. $rules[] = array("4302", 9, 9);
  124. $rules[] = array("4303", 9, 9);
  125. $rules[] = array("4304", 9, 9);
  126. $rules[] = array("4305", 9, 9);
  127. $rules[] = array("431", 9, 9);
  128. $rules[] = array("432", 9, 9);
  129. $rules[] = array("433", 9, 9);
  130. $rules[] = array("434", 9, 9);
  131. $rules[] = array("435", 9, 9);
  132. $rules[] = array("4350", 9, 9);
  133. $rules[] = array("437", 9, 9);
  134. $rules[] = array("438", 9, 9);
  135. $rules[] = array("439", 9, 9);
  136. $rules[] = array("447", 9, 9);
  137. $rules[] = array("448", 9, 9);
  138. $rules[] = array("449", 9, 9);
  139. $rules[] = array("449", 9, 9);
  140. $rules[] = array("450", 9, 9);
  141. $rules[] = array("451", 9, 9);
  142. $rules[] = array("457", 9, 9);
  143. $rules[] = array("458", 9, 9);
  144. $rules[] = array("4590", 9, 9);
  145. $rules[] = array("4591", 9, 9);
  146. $rules[] = array("4592", 9, 9);
  147. $rules[] = array("466", 9, 9);
  148. $rules[] = array("488", 9, 9);
  149. // end mobile phone numbers
  150. $rules[] = array("50", 9, 9);
  151. $rules[] = array("51", 9, 9);
  152. $rules[] = array("52", 9, 9);
  153. $rules[] = array("53", 9, 9);
  154. $rules[] = array("54", 9, 9);
  155. $rules[] = array("55", 9, 9);
  156. $rules[] = array("56", 9, 9);
  157. $rules[] = array("57", 9, 9);
  158. $rules[] = array("58", 9, 9);
  159. //$rules[] = array("59", 9, 9); //Not for use after 31 December 2005
  160. //$rules[] = array("7", 9, 9); // More specific rules follow
  161. $rules[] = array("73", 9, 9);
  162. $rules[] = array("740", 9, 9);
  163. $rules[] = array("741", 9, 9);
  164. $rules[] = array("745", 9, 9);
  165. $rules[] = array("746", 9, 9);
  166. $rules[] = array("747", 9, 9);
  167. $rules[] = array("749", 9, 9);
  168. $rules[] = array("754", 9, 9);
  169. $rules[] = array("755", 9, 9);
  170. $rules[] = array("756", 9, 9);
  171. //$rules[] = array("8", 9, 9); // More specific rules follow
  172. $rules[] = array("86", 9, 9);
  173. $rules[] = array("871", 9, 9);
  174. $rules[] = array("872", 9, 9);
  175. $rules[] = array("873", 9, 9);
  176. $rules[] = array("874", 9, 9);
  177. $rules[] = array("880", 9, 9);
  178. $rules[] = array("881", 9, 9);
  179. $rules[] = array("882", 9, 9);
  180. $rules[] = array("883", 9, 9);
  181. $rules[] = array("884", 9, 9);
  182. $rules[] = array("885", 9, 9);
  183. $rules[] = array("886", 9, 9);
  184. $rules[] = array("887", 9, 9);
  185. $rules[] = array("888", 9, 9);
  186. $rules[] = array("889", 9, 9);
  187. $rules[] = array("890", 9, 9);
  188. $rules[] = array("891", 9, 9);
  189. $rules[] = array("892", 9, 9);
  190. $rules[] = array("893", 9, 9);
  191. $rules[] = array("894", 9, 9);
  192. $rules[] = array("895", 9, 9);
  193. $rules[] = array("896", 9, 9);
  194. $rules[] = array("897", 9, 9);
  195. $rules[] = array("898", 9, 9);
  196. $rules[] = array("899", 9, 9);
  197. foreach ($rules as $rule) {
  198. if (preg_match('/^'.$rule[0].'/', $phonenumber) && strlen($phonenumber) >= $rule[1] && strlen($phonenumber) <= $rule[2]) {
  199. return TRUE;
  200. }
  201. }
  202. return FALSE;
  203. }
  204. /**
  205. * Formatting for Australian Phone Numbers. Based upon ITU-T E.123 (but let's not get too crazy)
  206. *
  207. * @param string $phonenumber
  208. * @return string Returns a string containing the phone number with some formatting.
  209. */
  210. function format_au_phone_number($phonenumber, $field = FALSE) {
  211. $prefix = '';
  212. $extension = '';
  213. // strip old formatting chars
  214. $phonenumber = preg_replace('/[\-() ]/', '', $phonenumber);
  215. /*
  216. * strip and save the +61 prefix if found
  217. */
  218. if (preg_match('/^\+61/', $phonenumber, $match)) {
  219. $prefix = '+61 ';
  220. $phonenumber = str_replace('+61', '', $phonenumber);
  221. }
  222. /*
  223. * strip and save the extension (x9999) postfix if found
  224. */
  225. if (preg_match('/(x[0-9]+)$/', $phonenumber, $match)) {
  226. $extension = ' ('.$match[1].')';
  227. $phonenumber = preg_replace('/x[0-9]+$/', '', $phonenumber);
  228. }
  229. /*
  230. * geographic numbers and UPT
  231. * Eg. (02) 9999 9999 or +61 (2) 9999 9999
  232. */
  233. if (preg_match('/^(0{0,1}[23578])([0-9]{4})([0-9]{4})$/', $phonenumber, $match)) {
  234. return $prefix . '(' . $match[1] . ') ' . $match[2] . ' ' . $match[3] . $extension;
  235. }
  236. /*
  237. * mobile numbers
  238. * Eg. 0423 999 999 or +61 423 999 999
  239. */
  240. if (preg_match('/^(0{0,1}4[0-9]{2})([0-9]{3})([0-9]{3})$/', $phonenumber, $match)) {
  241. return $prefix . $match[1] . ' ' . $match[2] . ' ' . $match[3] . $extension;
  242. }
  243. /*
  244. * 10 digit numbers
  245. * Eg. 1800 999 999
  246. */
  247. if (preg_match('/^([0-9]{4})([0-9]{3})([0-9]{3})$/', $phonenumber, $match)) {
  248. return $prefix . $match[1] . ' ' . $match[2] . ' ' . $match[3] . $extension;
  249. }
  250. /*
  251. * 9 digit satellite or dialup data numbers
  252. * Eg. 1471 99999
  253. */
  254. if (preg_match('/^(14[0-9]{2}|1983)([0-9]{5})$/', $phonenumber, $match)) {
  255. return $prefix . $match[1] . ' ' . $match[2] . $extension;
  256. }
  257. /*
  258. * 6 digit numbers
  259. * Eg. 13 99 99
  260. */
  261. if (preg_match('/^([0-9]{2})([0-9]{2})([0-9]{2})$/', $phonenumber, $match)) {
  262. return $prefix . $match[1] . ' ' . $match[2] . ' ' . $match[3] . $extension;
  263. }
  264. // default
  265. return $prefix . $phonenumber . $extension;
  266. }