phone.es.inc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Spanish phone numbers.
  5. */
  6. function phone_es_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Spanish phone number<br>Spanish phone numbers should only contains numbers and spaces and be like 999 999 999',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is a valid nine-digit Spanish phone number
  14. *
  15. * @param string $phonenumber
  16. * @return boolean Returns boolean FALSE if the phone number is not valid.
  17. */
  18. function valid_es_phone_number($phonenumber) {
  19. $phonenumber = trim($phonenumber);
  20. // define regular expression
  21. //$regex = "/
  22. // \D* # optional separator
  23. // [69]\d{2} # first group of numbers
  24. // \D* # optional separator
  25. // \d{3} # second group
  26. // \D* # optional separator
  27. // \d{3} # third group
  28. // \D* # ignore trailing non-digits
  29. // $/x";
  30. $regex = '/^[0-9]{2,3}-? ?[0-9]{6,7}$/';
  31. // return true if valid, false otherwise
  32. return (bool) preg_match($regex, $phonenumber);
  33. }
  34. /**
  35. * Convert a valid Spanish phone number into standard (+34) 916 555 777 format
  36. *
  37. * @param $phonenumber must be a valid nine-digit number (with optional international prefix)
  38. *
  39. */
  40. function format_es_phone_number($phonenumber, $field = FALSE) {
  41. // define regular expression
  42. //$regex = "/
  43. // \D* # optional separator
  44. // ([69]\d{2}) # first group of numbers
  45. // \D* # optional separator
  46. // (\d{3}) # second group
  47. // \D* # optional separator
  48. // (\d{3}) # third group
  49. // \D* # ignore trailing non-digits
  50. // $/x";
  51. $regex = '/^[0-9]{2,3}-? ?[0-9]{6,7}$/';
  52. // get digits of phone number
  53. preg_match($regex, $phonenumber, $matches);
  54. // construct ten-digit phone number
  55. $phonenumber = $matches[1] . ' ' . $matches[2] . ' ' . $matches[3];
  56. return $phonenumber;
  57. }