phone.sg.inc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Singapore phone numbers.
  5. */
  6. function phone_sg_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Singaporean phone number<br>Singaporean 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_sg_phone_number($phonenumber) {
  19. // define regular expression
  20. /*
  21. See: http://en.wikipedia.org/wiki/Telephone_numbers_in_Singapore
  22. Accepts:
  23. +6561234567 / +6581234567 / +6591234567
  24. +65 61234567 / +65 81234567 / +65 91234567
  25. 61234567 / 81234567 / 91234567
  26. */
  27. $regex = '/^(\+65)?\s?[689]\d{7}$/i';
  28. // return true if valid, false otherwise
  29. return (bool) preg_match($regex, $phonenumber);
  30. }
  31. /**
  32. * Formatting for Singapore Phone Numbers.
  33. *
  34. * @param string $phonenumber
  35. * @return string Returns a string containting the phone number with some formatting.
  36. */
  37. function format_sg_phone_number($phonenumber, $field) {
  38. //$phonenumber = trim($phonenumber);
  39. // do some formatting on the phone number
  40. /* ==> to be done ==> add the country code
  41. if ($field['phone_country_code']) {
  42. if ($matches[1] != "+39") {
  43. $phonenumber = "+39" . " " . $phonenumber;
  44. }
  45. }
  46. */
  47. return $phonenumber;
  48. }