phone.fr.inc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for French phone numbers.
  5. */
  6. define('PHONE_FR_REGEX', '/(\+33|0)([1-9]\d{8}|85\d{7}|87[0-57-9]\d{6})$/');
  7. function phone_fr_metadata() {
  8. // These strings are translated using t() on output.
  9. return array(
  10. 'error' => '"%value" is not a valid French phone number<br>French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99',
  11. );
  12. }
  13. /**
  14. * Verification for French Phone Numbers.
  15. * According to http://www.itu.int/itudoc/itu-t/number/f/fra/70680.html
  16. * (Released 2006/01/26, retrieved 2008/08/12)
  17. *
  18. * @param string $phonenumber
  19. * @return boolean Returns boolean FALSE if the phone number is not valid.
  20. */
  21. function valid_fr_phone_number($phonenumber) {
  22. //$phonenumber = trim($phonenumber);
  23. $phonenumber = str_replace(array(' ','-','(',')'), '', $phonenumber);
  24. return (bool) preg_match(PHONE_FR_REGEX, $phonenumber);
  25. }
  26. /**
  27. * Formatting for French Phone Numbers.
  28. *
  29. * @param string $phonenumber
  30. * @return string Returns a string containting the phone number with some formatting.
  31. */
  32. function format_fr_phone_number($phonenumber, $field = FALSE) {
  33. $phone = str_replace(array(' ','-','(',')'), '', $phonenumber);
  34. if (preg_match(PHONE_FR_REGEX, $phone, $matches) != 1) {
  35. return $phonenumber; // not a french phone number
  36. }
  37. return ($field && $field['phone_country_code'] ? '+33 ' : '0') . $matches[2];
  38. }