phone.il.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. //drived from au module and manipulated by Moshe Beeri, email: moshe.beeri (at-shetrudel) google mail
  3. /**
  4. * @file
  5. * CCK Field for Isreali phone numbers.
  6. */
  7. function phone_il_metadata() {
  8. return array(
  9. 'error' => '"%value" is not a valid Israeli phone number',
  10. );
  11. }
  12. /**
  13. * Verification for Israel Phone Numbers.
  14. *
  15. * @param string $phonenumber
  16. * @return boolean Returns boolean FALSE if the phone number is not valid.
  17. */
  18. function valid_il_phone_number($phonenumber) {
  19. //$phonenumber = trim($phonenumber);
  20. // strip formatting chars
  21. $phonenumber = preg_replace('/[\-() ]/', '', $phonenumber);
  22. // strip optional '+972' or '0' prefixes
  23. $phonenumber = preg_replace('/^(\+972)/', '', $phonenumber);
  24. //$rules[] = array("Prefix","Minimum length","Maximum length");
  25. //http://he.wikipedia.org/wiki/%D7%A7%D7%99%D7%93%D7%95%D7%9E%D7%AA_%D7%98%D7%9C%D7%A4%D7%95%D7%9F
  26. $rules[] = array("02", 7, 10);
  27. $rules[] = array("03", 7, 10);
  28. $rules[] = array("04", 7, 10);
  29. $rules[] = array("08", 7, 10);
  30. $rules[] = array("09", 7, 10);
  31. $rules[] = array("072", 7, 10);
  32. $rules[] = array("073", 7, 10);
  33. $rules[] = array("074", 7, 10);
  34. $rules[] = array("076", 7, 10);
  35. $rules[] = array("077", 7, 10);
  36. $rules[] = array("078", 7, 10);
  37. $rules[] = array("050", 7, 10);
  38. $rules[] = array("052", 7, 10);
  39. $rules[] = array("054", 7, 10);
  40. $rules[] = array("057", 7, 10);
  41. $rules[] = array("1800", 6, 10);
  42. $rules[] = array("1801", 6, 10);
  43. $rules[] = array("1700", 6, 10);
  44. foreach ($rules as $rule) {
  45. if (preg_match('/^'.$rule[0].'/', $phonenumber) && strlen($phonenumber) >= $rule[1] && strlen($phonenumber) <= $rule[2]) {
  46. return TRUE;
  47. }
  48. }
  49. return FALSE;
  50. }
  51. /**
  52. * Formatting for Israeli Phone Numbers. Based upon ITU-T E.123 (but let's not get too crazy)
  53. *
  54. * @param string $phonenumber
  55. * @return string Returns a string containing the phone number with some formatting.
  56. */
  57. function format_il_phone_number($phonenumber) {
  58. $prefix = '';
  59. $extension = '';
  60. // strip old formatting chars
  61. $phonenumber = preg_replace('/[\-() ]/', '', $phonenumber);
  62. /*
  63. * strip and save the +9726 prefix if found
  64. */
  65. if (preg_match('/^\+972/', $phonenumber, $match)) {
  66. $prefix = '+972 ';
  67. $phonenumber = str_replace('+972', '', $phonenumber);
  68. }
  69. /*
  70. * 9 phones digit numbers
  71. * Eg. 03 9999 999
  72. */
  73. if (preg_match('/^([0-9]{2})([0-9]{4})([0-9]{3})$/', $phonenumber, $match)) {
  74. return $prefix . $match[1] . ' ' . $match[2] . ' ' . $match[3] . $extension;
  75. }
  76. /*
  77. * 10 digit numbers
  78. * Eg. 1800 999 999
  79. */
  80. if (preg_match('/^([0-9]{4})([0-9]{3})([0-9]{3})$/', $phonenumber, $match)) {
  81. return $prefix . $match[1] . ' ' . $match[2] . ' ' . $match[3] . $extension;
  82. }
  83. /*
  84. * cell phone
  85. * Eg. 054 9999 999
  86. */
  87. if (preg_match('/^([0-9]{3})([0-9]{4})([0-9]{3})$/', $phonenumber, $match)) {
  88. return $prefix . $match[1] . ' ' . $match[2] . ' ' . $match[3] . $extension;
  89. }
  90. // default
  91. return $prefix . $phonenumber . $extension;
  92. }