phone.pa.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * CCK field for Panamanian phone numbers
  5. */
  6. /*
  7. * ((00|\+)?[0-9]{3}[\s])? #First group 00507 or +507 (plus space)
  8. * ([0-9]{3,4}) #Second group three or four numbers (four for cellphones)
  9. * [\s|-]? space or dash
  10. * ([0-9]{4}) #Third group
  11. *
  12. * Accepted:
  13. * 00507 2603133
  14. * +507 260-4343
  15. * 260 3133
  16. * 260-3133
  17. *
  18. * Cellphones
  19. * +507 6545-4345
  20. * 6545-4345
  21. * 6545 4345
  22. * 65454345
  23. */
  24. define('PHONE_PA_REGEX', '/((00|\+)?[0-9]{3}[\s])?([0-9]{3,4})[\s|-]?([0-9]{4})/');
  25. function phone_pa_metadata() {
  26. // These strings are translated using t() on output.
  27. return array(
  28. 'error' => '"%value" is not a valid Panamanian phone number!<br>Panamanian phone numbers should contain only numbers, spaces and dashes be like 9999-999, 9999 999 or 9999999 with an optional prefix of "+507" or "00507".',
  29. );
  30. }
  31. /**
  32. * Verifies that $phonenumber is a valid nine-digit Panamanian phone number
  33. *
  34. * @param string $phonenumber
  35. * @return boolean Returns boolean FALSE if the phone number is not valid.
  36. */
  37. function valid_pa_phone_number($phonenumber) {
  38. //$phonenumber = trim($phonenumber);
  39. // define regular expression
  40. // return true if valid, false otherwise
  41. return (bool) preg_match(PHONE_PA_REGEX, $phonenumber);
  42. }
  43. /**
  44. * Convert a valid Panamenian phone number into standard (+507) 260-4324 format
  45. *
  46. * @param $phonenumber must be a valid nine-digit number (with optional international prefix)
  47. *
  48. */
  49. function format_pa_phone_number($phonenumber, $field = FALSE) {
  50. // get digits of phone number
  51. preg_match(PHONE_PA_REGEX, $phonenumber, $matches);
  52. if (preg_match(PHONE_PA_REGEX, $phonenumber, $matches) != 1) {
  53. return $phonenumber; // not a Panamanian phone number
  54. }
  55. $phonenumber = $matches[3] . '-' . $matches[4];
  56. if (trim($matches[1]) != '') {
  57. $phonenumber = '+' . substr($matches[1], -4) . $phonenumber;
  58. }
  59. elseif ($field && isset($field['phone_country_code'])) {
  60. $phonenumber = '+507 ' . $phonenumber;
  61. }
  62. return $phonenumber;
  63. }