phone.be.inc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Belgian phone numbers.
  5. */
  6. function phone_be_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Belgian phone number<br>Belgian 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_be_phone_number($phonenumber) {
  19. // define regular expression
  20. $regex = "/^(\+32|0)[1-9]\d{7,8}$/i";
  21. $phonenumber = str_replace(array(' ','-','(',')'), '', $phonenumber);
  22. // return true if valid, false otherwise
  23. return (bool) preg_match($regex, $phonenumber);
  24. }
  25. /**
  26. * Formatting for Belgian Phone Numbers.
  27. *
  28. * @param string $phonenumber
  29. * @return string Returns a string containting the phone number with some formatting.
  30. */
  31. function format_be_phone_number($phonenumber, $field) {
  32. $phonenumber = trim($phonenumber);
  33. if ($field['phone_country_code']) {
  34. if ($matches[1] != "+32") {
  35. $phonenumber = "+32" . " " . $phonenumber;
  36. }
  37. }
  38. return $phonenumber;
  39. }