phone.cs.inc 1.4 KB

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