phone.ca.inc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Canadian phone numbers.
  5. */
  6. function phone_ca_metadata() {
  7. return array(
  8. 'error' => '"%value" is not a valid North American phone number<br>North American Phone numbers should only contain numbers and + and - and ( and ) and spaces and be like 999-999-9999. Please enter a valid ten-digit phone number with optional extension.',
  9. );
  10. }
  11. /**
  12. * Verifies that $phonenumber is a valid ten-digit North American phone number
  13. *
  14. * @param string $phonenumber
  15. * @return boolean Returns boolean FALSE if the phone number is not valid.
  16. */
  17. function valid_ca_phone_number($phonenumber) {
  18. $phonenumber = trim($phonenumber);
  19. // define regular expression
  20. $regex = '/
  21. \D* # ignore non-digits
  22. (\d*) # an optional 1
  23. \D* # optional separator
  24. [2-9][0-8]\d # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
  25. \D* # optional separator
  26. [2-9]\d{2} # 3-digit prefix (cannot start with 0 or 1)
  27. \D* # optional separator
  28. \d{4} # 4-digit line number
  29. \D* # optional separator
  30. \d* # optional extension
  31. \D* # ignore trailing non-digits
  32. /x';
  33. // return true if valid, false otherwise
  34. $result = preg_match($regex, $phonenumber, $matches);
  35. return ($result && ($matches[1] == '' || $matches[1] == '1'));
  36. }
  37. /**
  38. * Convert a valid North American phone number into standard (444) 867-5309 x1234 format
  39. *
  40. * @param $phonenumber must be a valid ten-digit number (with optional extension)
  41. *
  42. */
  43. function format_ca_phone_number($phonenumber, $field) {
  44. // define regular expression
  45. $regex = '/
  46. \D* # ignore non-digits
  47. (\d*) # an optional 1
  48. \D* # optional separator
  49. ([2-9][0-8]\d) # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
  50. \D* # optional separator
  51. ([2-9]\d{2}) # 3-digit prefix (cannot start with 0 or 1)
  52. \D* # optional separator
  53. (\d{4}) # 4-digit line number
  54. \D* # optional separator
  55. (\d*) # optional extension
  56. \D* # ignore trailing non-digits
  57. /x';
  58. // get digits of phone number
  59. preg_match($regex, $phonenumber, $matches);
  60. $separator = isset($field['ca_phone_separator']) ? $field['ca_phone_separator'] : '-';
  61. // construct ten-digit phone number
  62. $phonenumber =
  63. ( $field['ca_phone_parentheses'] ?
  64. '(' . $matches[2] . ') ' :
  65. $matches[2] . $separator ) .
  66. $matches[3] . $separator . $matches[4];
  67. // Optional extension
  68. if ($matches[5] != '') {
  69. $phonenumber .= ' x' . $matches[5];
  70. }
  71. if ($field['phone_country_code']) {
  72. // This condition check is pointless.
  73. if ($matches[1] != '1') {
  74. $phonenumber = '1' . ' ' . $phonenumber;
  75. }
  76. }
  77. return $phonenumber;
  78. }