phone.sn.inc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Senegalese phone numbers.
  5. */
  6. define('PHONE_SN_REGEX', '/((\+221|00221)?)((7[7608][0-9]{7}$)|(3[03][98][0-9]{6}$))/');
  7. function phone_sn_metadata() {
  8. // These strings are translated using t() on output.
  9. return array(
  10. 'error' => '"%value" is not a valid Senegalese phone number',
  11. );
  12. }
  13. /**
  14. * Verification for Senegalese Phone Numbers.
  15. *
  16. * @param string $phonenumber
  17. * @return boolean Returns boolean FALSE if the phone number is not valid.
  18. */
  19. function valid_sn_phone_number($phonenumber) {
  20. $phonenumber = str_replace(array(' ', '-', '(', ')') , '', $phonenumber);
  21. return (bool) preg_match(PHONE_SN_REGEX, $phonenumber);
  22. }
  23. /**
  24. * Formatting for Senegalese Phone Numbers.
  25. *
  26. * @param string $phonenumber
  27. * @return string Returns the phone number as string.
  28. */
  29. function format_sn_phone_number($phonenumber, $field = FALSE) {
  30. $phone = str_replace(array(' ', '-', '(', ')'), '', $phonenumber);
  31. if (preg_match(PHONE_SN_REGEX, $phone, $matches) != 1) {
  32. return $phonenumber; // not a french phone number
  33. }
  34. //
  35. if (in_array($matches[0], array('+221', '00221'))) {
  36. return $matches[2];
  37. }
  38. else {
  39. return $matches[0];
  40. }
  41. }