phone.it.inc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Italian phone numbers.
  5. */
  6. function phone_it_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Italian phone number<br>Italian phone numbers should only ...',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is valid
  14. *
  15. * @param string $phonenumber
  16. * @return boolean Returns boolean FALSE if the phone number is not valid.
  17. */
  18. function valid_it_phone_number($phonenumber) {
  19. // define regular expression
  20. $regex = "/^(\+39)?[ ]?([0-9]{2,3}(\/|-| )?[0-9]{6,7})$/i";
  21. // return true if valid, false otherwise
  22. return (bool) preg_match($regex, $phonenumber);
  23. }
  24. /**
  25. * Formatting for Italian Phone Numbers.
  26. *
  27. * @param string $phonenumber
  28. * @return string Returns a string containting the phone number with some formatting.
  29. */
  30. function format_it_phone_number($phonenumber, $field) {
  31. //$phonenumber = trim($phonenumber);
  32. // do some formatting on the phone number
  33. $phonenumber = str_replace("[ -]", "", $phonenumber);
  34. if ($field['phone_country_code']) {
  35. if ($matches[1] != "+39") {
  36. $phonenumber = "+39" . " " . $phonenumber;
  37. }
  38. }
  39. return $phonenumber;
  40. }