phone.cr.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Costa Rican phone numbers.
  5. */
  6. function phone_cr_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Costa Rican phone number!<br>Costa Rican phone numbers should contain only numbers and spaces be like 99 99 99 99 with an optional prefix of "+506" or "00506".',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is a valid eight-digit Costa Rican phone number
  14. *
  15. * @param string $phonenumber
  16. * @return boolean Returns boolean FALSE if the phone number is not valid.
  17. */
  18. function valid_cr_phone_number($phonenumber) {
  19. //$phonenumber = trim($phonenumber);
  20. // define regular expression
  21. $regex = "/(00)?[\s|-]?((\+)?[\s|-]?[0-9]{3})?[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?/";
  22. // return true if valid, false otherwise
  23. return (bool) preg_match($regex, $phonenumber);
  24. }
  25. /**
  26. * Convert a valid Costa Rican phone number into standard (+506) 5555 55 55 format
  27. *
  28. * @param $phonenumber must be a valid eight-digit number (with optional international prefix)
  29. *
  30. */
  31. /*
  32. Accepts:
  33. +506 88798857
  34. +506 88-79-88-57
  35. 00506 88798857
  36. 00506 88-79-88-57
  37. Rejects:
  38. +506 8 8798857
  39. +506 8 8-79-88-57
  40. 00506 8 8798857
  41. 00506 8 8-79-88-57 */
  42. function format_cr_phone_number($phonenumber, $field = FALSE) {
  43. // define regular expression
  44. $regex = "/(00)?[\s|-]?((\+)?[\s|-]?[0-9]{3})?[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?/";
  45. // get digits of phone number
  46. //dprint_r($matches);
  47. preg_match($regex, $phonenumber, $matches);
  48. // construct eight-digit phone number
  49. //dprint_r($matches);
  50. $phonenumber = $matches[4] . '-' . $matches[5] . '-' . $matches[6] . '-' . $matches[7];
  51. if($matches[2]){
  52. if($matches[1])
  53. $phonenumber = "+" . $matches[2] . " " . $phonenumber;
  54. else
  55. $phonenumber = $matches[2] . " " . $phonenumber;
  56. }
  57. return $phonenumber;
  58. }