phone.nl.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Dutch phone numbers.
  5. */
  6. function phone_nl_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Dutch phone number!<br>Dutch phone numbers should contain only numbers and spaces and - and be like 099-9999999 with an optional prefix of "+31".',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is a valid ten-digit Dutch phone number with spaces and -
  14. *
  15. * Regular expression adapted from Nico Lubbers'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_nl_phone_number($phonenumber) {
  21. //$phonenumber = trim($phonenumber);
  22. /*
  23. Accepts:
  24. 06-12345678
  25. 06 123 456 78
  26. 010-1234567
  27. 020 123 4567
  28. 0221-123456
  29. 0221 123 456
  30. Rejects:
  31. 05-12345678
  32. 123-4567890
  33. 123 456 7890
  34. */
  35. // define regular expression
  36. $regex = '/
  37. ([0]{1}[6]{1}[-\s]+[1-9]{1}[\s]*([0-9]{1}[\s]*){7}) # Mobile phonenumber
  38. |
  39. ([0]{1}[1-9]{1}[0-9]{2}[-\s]+[1-9]{1}[\s]*([0-9]{1}[\s]*){5}) # Phonenumber with 4 digit area code
  40. |
  41. ([0]{1}[1-9]{1}[0-9]{1}[-\s]+[1-9]{1}[\s]*([0-9]{1}[\s]*){6}) # Phonenumber with 3 digit area code
  42. /x';
  43. // return true if valid, false otherwise
  44. return (bool) preg_match($regex, $phonenumber);
  45. }
  46. /**
  47. * Formatting for Dutch Phone Numbers into standard area-phonenumber or with extra country code +31 international format
  48. *
  49. * @param string $phonenumber
  50. * @return string Returns a string containting the phone number with some formatting.
  51. */
  52. function format_nl_phone_number($phonenumber, $field) {
  53. $areacode = $localnumber = '';
  54. // Mobile number
  55. if (preg_match('/([0]{1}[6]{1}[-\s]+[1-9]{1}[\s]*([0-9]{1}[\s]*){7})/', $phonenumber)) {
  56. preg_match('/([0]{1}[6]{1})[-\s]+([1-9]{1}[\s]*([0-9]{1}[\s]*){7})/', $phonenumber, $matches);
  57. }
  58. // Phonenumber with 4 digit area code
  59. if (preg_match('/([0]{1}[1-9]{1}[0-9]{2}[-\s]+[1-9]{1}[\s]*([0-9]{1}[\s]*){5})/', $phonenumber)) {
  60. preg_match('/([0]{1}[1-9]{1}[0-9]{2})[-\s]+([1-9]{1}[\s]*([0-9]{1}[\s]*){5})/', $phonenumber, $matches);
  61. }
  62. // Phonenumber with 3 digit area code
  63. if (preg_match('/([0]{1}[1-9]{1}[0-9]{1}[-\s]+[1-9]{1}[\s]*([0-9]{1}[\s]*){6})/', $phonenumber)) {
  64. preg_match('/([0]{1}[1-9]{1}[0-9]{1})[-\s]+([1-9]{1}[\s]*([0-9]{1}[\s]*){6})/', $phonenumber, $matches);
  65. }
  66. $areacode = $matches[1];
  67. $localnumber = preg_replace('/ /', '', $matches[2]);
  68. $phonenumber = $areacode. '-'. $localnumber;
  69. // Add Country code if needed
  70. if ($field['phone_country_code']) {
  71. $areacode = preg_replace('/^0/', '', $areacode);
  72. $phonenumber = '+31-'. $areacode. '-'. $localnumber;
  73. }
  74. return $phonenumber;
  75. }