phone.gb.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for British phone numbers.
  5. */
  6. function phone_gb_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid British (UK) phone number<br>British Phone numbers should .... ',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is a valid eleven-digit United Kingdom phone number
  14. *
  15. * Regular expression adapted from Amos Hurd's regex at RegExLib.com
  16. *
  17. * @param string $phonenumber
  18. * @return boolean Returns boolean FALSE if the phone number is not valid.
  19. */
  20. function valid_gb_phone_number($phonenumber) {
  21. /*
  22. Accepts:
  23. +441970123456
  24. +44(0)1970123456
  25. +44 1970 123 456
  26. +44 (0)1970 123 456
  27. (01970) 123456 #0001
  28. Rejects:
  29. (+441970)123456
  30. +44(1970)123456
  31. +44 01970 123 456
  32. (0197) 0123456 #01
  33. */
  34. $regex = "/
  35. (
  36. (^\+44\s?(\(0\))?\d{4}|^\(?0\d{4}\)?){1}\s?\d{3}\s?\d{3} # 4 digit area code with optional +44 internationalisation or not, optional spaces and brackets.
  37. |
  38. (^\+44\s?(\(0\))?\d{3}|^\(?0\d{3}\)?){1}\s?\d{3}\s?\d{4} # 3 digit area code with optional +44 internationalisation or not, optional spaces and brackets.
  39. |
  40. (^\+44\s?(\(0\))?\d{2}|^\(?0\d{2}\)?){1}\s?\d{4}\s?\d{4} # 2 digit area code with optional +44 internationalisation or not, optional spaces and brackets.
  41. |
  42. (^\+44\s?(\(0\))?\d{1}|^\(?0\d{1}\)?){1}\s?\d{5}\s?\d{5} # 1 digit area code with optional +44 internationalisation or not, optional spaces and brackets.
  43. )
  44. (\s?\#\d*)? # optional extension number shown with a hash divider
  45. /x";
  46. if (!preg_match($regex, $phonenumber)) {
  47. return FALSE;
  48. }
  49. else
  50. {
  51. return TRUE;
  52. }
  53. }
  54. /**
  55. * Convert a valid United Kingdom phone number into standard +44 (0)1970 123 456 #001 international format
  56. *
  57. * @param $phonenumber must be a valid eleven-digit number (with optional extension)
  58. *
  59. */
  60. function format_gb_phone_number($phonenumber, $field = FALSE) {
  61. $area = $number = $extension = '';
  62. //If we already have the formatting we want just return
  63. if (preg_match(
  64. "/
  65. (
  66. \+44\s\(0\)\d{4}\s\d{3}\s\d{3} # 4 digit area code
  67. |
  68. \+44\s\(0\)\d{3}\s\d{3}\s\d{4} # 3 digit area code
  69. |
  70. \+44\s\(0\)\d{2}\s\d{4}\s\d{4} # 2 digit area code
  71. )
  72. (\s\#\d*)?
  73. /",$phonenumber)) {
  74. return $phonenumber;
  75. }
  76. else {
  77. //Simplify to 10 digit number and clean up ready for international reformat.
  78. $phonenumber = preg_replace("/^(\+44)?\s?(\(?0\)?)?/","",$phonenumber);
  79. $phonenumber = preg_replace("/\(/","",$phonenumber);
  80. $phonenumber = preg_replace("/\(0/","",$phonenumber);
  81. $phonenumber = preg_replace("/\)/","",$phonenumber);
  82. //If there are some spaces in the number assume some level of preformatting
  83. if(preg_match("/ /",$phonenumber)) {
  84. $regex = "/
  85. # 4 digit area code.
  86. (
  87. (\d{4}) # capture 4 digit area code
  88. \s+ # ignore required separator to make a distinction with other area codes
  89. (\d{3}) # capture first set of numbers in the local number
  90. \s* # ignore optional separator
  91. (\d{3}) # capture second set of numbers in the local number
  92. |
  93. # 3 digit area code.
  94. (\d{3}) # capture 3 digit area code
  95. \s+ # ignore required seperator
  96. (\d{3}) # capture first set of numbers in the local number
  97. \s* # ignore possible boundary
  98. (\d{4}) # capture second set of numbers in the local number
  99. |
  100. # 2 digit area code.
  101. (\d{2}) # capture 2 digit area code
  102. \s+ # ignore required boundary to make a distinction with other area codes
  103. (\d{4}) # capture first set of numbers in the local number
  104. \s* # ignore possible boundary
  105. (\d{4}) # capture second set of numbers in the local number
  106. )
  107. # capture the optional extension number
  108. (\s*\#)?
  109. (\d{4}|\d{3})?
  110. /x";
  111. preg_match($regex, $phonenumber, $matches);
  112. $area = $matches[2].$matches[5].$matches[8];
  113. $number = $matches[3].$matches[6].$matches[9].' '.$matches[4].$matches[7].$matches[10];
  114. $extension = $matches[12];
  115. }
  116. //If there are no spaces in the number assume 4 digit area code.
  117. else {
  118. preg_match("/(\d{4})(\d{3})(\d{3})\#?(\d*)?/",$phonenumber, $matches);
  119. $area = $matches[1];
  120. $number = $matches[2].' '.$matches[3];
  121. $extension = $matches[4];
  122. }
  123. if ($field['phone_country_code']) {
  124. $phonenumber = '+44 (0)'.$area.' '.$number;
  125. }
  126. else {
  127. $phonenumber = '0'.$area.' '.$number;
  128. }
  129. $phonenumber .= (empty($extension))?'':" #$extension";
  130. }
  131. return $phonenumber;
  132. }