phone.za.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for South African phone numbers.
  5. */
  6. function phone_za_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid South African phone number!<br>South African phone numbers should only contain numbers with an optional prefix of "+27".',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is a valid South African phone number
  14. *
  15. * @param string $phonenumber
  16. * @return boolean Returns boolean FALSE if the phone number is not valid.
  17. */
  18. function valid_za_phone_number($phonenumber) {
  19. $phonenumber = trim($phonenumber);
  20. // define regular expression
  21. $regex = '/^((?:\+27|27)|0)[ ]*((\d{2})(-| )?(\d{3})(-| )?(\d{4})|(\d{2})( |-)(\d{7}))$/';
  22. // return true if valid, false otherwise
  23. return (bool) preg_match($regex, $phonenumber);
  24. }
  25. /**
  26. * Convert a valid South African phone number into standard ... format
  27. *
  28. * @param $phonenumber must be a valid ... digit number (with optional international prefix)
  29. *
  30. */
  31. function format_za_phone_number($phonenumber, $field) {
  32. // define regular expression
  33. $regex = '/^((?:\+27|27)|0)[ ]*((\d{2})(-| )?(\d{3})(-| )?(\d{4})|(\d{2})( |-)(\d{7}))$/';
  34. // get digits of phone number
  35. preg_match($regex, $phonenumber, $matches);
  36. /*
  37. drupal_set_message('$matches[1] = ' . $matches[1], 'error');
  38. drupal_set_message('$matches[2] = ' . $matches[2], 'error');
  39. drupal_set_message('$matches[3] = ' . $matches[3], 'error');
  40. drupal_set_message('$matches[4] = ' . $matches[4], 'error');
  41. drupal_set_message('$matches[5] = ' . $matches[5], 'error');
  42. drupal_set_message('$matches[6] = ' . $matches[6], 'error');
  43. drupal_set_message('$matches[7] = ' . $matches[7], 'error');
  44. drupal_set_message('$matches[8] = ' . $matches[8], 'error');
  45. */
  46. if ($field['phone_country_code']) {
  47. $phonenumber = '+27' . ' ' . $matches[3] .'-'. $matches[5] .'-'. $matches[7];
  48. }
  49. else {
  50. $phonenumber = '0' . $matches[3] .'-'. $matches[5] .'-'. $matches[7];
  51. }
  52. return $phonenumber;
  53. }