phone.ru.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Russian phone numbers.
  5. */
  6. function phone_ru_metadata() {
  7. // These strings are translated using t() on output.
  8. return array(
  9. 'error' => '"%value" is not a valid Russian phone number<br>Russian Phone numbers should .... ',
  10. );
  11. }
  12. /**
  13. * Verifies that $phonenumber is a valid ten-digit Russian phone number
  14. *
  15. * @param string $phonenumber
  16. * @return boolean Returns boolean FALSE if the phone number is not valid.
  17. */
  18. function valid_ru_phone_number($phonenumber) {
  19. //$phonenumber = trim($phonenumber);
  20. // define regular expression
  21. $regex = "/
  22. \D* # ignore non-digits
  23. [78]? # an optional 78
  24. \D* # optional separator
  25. \d{3,5} # area code 3-5 digit
  26. \D* # optional separator
  27. \d{1,3} # 3-digit prefix
  28. \D* # optional separator
  29. \d{2} # 2-digit line number
  30. \D* # optional separator
  31. \d{2} # 2-digit line number
  32. \D* # ignore trailing non-digits
  33. /x";
  34. // return true if valid, false otherwise
  35. return (bool) preg_match($regex, $phonenumber);
  36. }
  37. /**
  38. * Convert a valid Russian phone number into standard +7 (495) 567-53-09 or +7 (444xx) 67-53-09 or mobile 8 910 414-56-90 format
  39. *
  40. * @param $phonenumber must be a valid ten-digit number (with optional extension)
  41. *
  42. */
  43. function format_ru_phone_number($phonenumber, $field = FALSE) {
  44. // define regular expression
  45. $regex = "/
  46. ^\D* # ignore non-digits
  47. ([78])? # an optional 78
  48. \D* # optional separator
  49. (\d{3,5}) # area code 3-5 digit
  50. \D* # optional separator
  51. (\d{1,3}) # capture 3-digit prefix
  52. \D* # optional separator
  53. (\d{2}) # 2-digit line number
  54. \D* # optional separator
  55. (\d{2}) # 2-digit line number
  56. \D* # ignore trailing non-digits
  57. /x";
  58. // get digits of phone number
  59. preg_match($regex, $phonenumber, $matches);
  60. // construct ten-digit phone number
  61. $phonenumber = $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . ' - ' . $matches[4] . ' - ' . $matches[5];
  62. return $phonenumber;
  63. }