phone.hu.inc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Hungarian phone numbers.
  5. */
  6. define('PHONE_HU_REGEX', "/^(\+36|06|)[\s.-]?([0-9]{1,2})[\s.-]?([0-9]{2,3})[\s.-]?([0-9]{2,4})$/");
  7. function phone_hu_metadata() {
  8. // These strings are translated using t() on output.
  9. return array(
  10. 'error' => '"%value" is not a valid Hungarian phone number!<br>Hungarian phone numbers should contain only numbers and spaces be like 70 999 9999 with an optional prefix of "+36" or "06".',
  11. );
  12. }
  13. /**
  14. * Verifies that $phonenumber is a valid nine-digit Hungarian phone number
  15. *
  16. * @param string $phonenumber
  17. * @return boolean Returns boolean FALSE if the phone number is not valid.
  18. */
  19. function valid_hu_phone_number($phonenumber) {
  20. $phonenumber = trim($phonenumber);
  21. // return true if valid, false otherwise
  22. return (bool) preg_match(PHONE_HU_REGEX, $phonenumber);
  23. }
  24. /**
  25. * Convert a valid Hungarian phone number into standard (+36) ..... format
  26. *
  27. * @param $phonenumber must be a valid nine-digit number (with optional international prefix)
  28. *
  29. */
  30. function format_hu_phone_number($phonenumber, $field = FALSE) {
  31. $phonenumber = trim($phonenumber);
  32. // get digits of phone number
  33. preg_match(PHONE_HU_REGEX, $phonenumber, $matches);
  34. $formatedphone = '';
  35. if ($field && $field['phone_country_code']) {
  36. $formatedphone .= '+36 ';
  37. }
  38. // construct ten-digit phone number
  39. $formatedphone .= $matches[2] . ' ' . $matches[3] . ' ' . $matches[4];
  40. return $formatedphone;
  41. }