phone.jo.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * This plugin is contributed by Untitled Web http://untitledstudios.com
  4. * @author Rashad Majali <rashad.612@gmail.com> http://drupal.org/user/319686
  5. * @file
  6. * CCK Field for Jordanian phone numbers.
  7. */
  8. function phone_jo_metadata(){
  9. // These strings are translated using t() on output.
  10. return array(
  11. 'error' => '"%value" is not a valid Jordanian phone number, Please check the spaces and dashes in your number.',
  12. );
  13. }
  14. /**
  15. * Verification for Jordanian Phone Numbers.
  16. *
  17. * @param string $phonenumber
  18. * @return boolean Returns boolean FALSE if the phone number is not valid.
  19. */
  20. function valid_jo_phone_number($phonenumber){
  21. /**
  22. Accepts:
  23. Mobile numbers: (X refers to network code, it might be 7,8,9).
  24. +9627X1234567
  25. +962-7X1234567
  26. +962 7X1234567
  27. 009627X1234567
  28. 00962-7X1234567
  29. 00962 7X1234567
  30. 962... accepted as well
  31. 07X1234567
  32. Local area numbers: (X refers to region code, i.e. Amman[6], north [2], middle [5], south[3]).
  33. +962X1234567
  34. +962-X-1234567
  35. +962X-1234567
  36. +962 X 1234567
  37. +962X 1234567
  38. +962 X1234567
  39. 00962X1234567
  40. 00962-X-1234567
  41. 00962X-1234567
  42. 00962 X 1234567
  43. 00962X 1234567
  44. 00962 X1234567
  45. 962... accepted as well
  46. 0X1234567
  47. 0X-1234567
  48. 0X 1234567
  49. Rejects:
  50. Generally rejects any number without leading code.
  51. starts with X instead of 0X
  52. Mobile:
  53. 7X1234567
  54. 7 X1234567 and similar formats
  55. +962 7 X1234567 and similar formats
  56. Local:
  57. X1234567
  58. X-1234567
  59. X 1234567 and similar formats
  60. */
  61. $regex = "/(^(\+962|00962|962|0)[-\s]{0,1}[7]{1}[7-9]{1}[0-9]{7}$) | (^(\+962|00962|962|0)[-\s]{0,1}[2-6][-\s]{0,1}[0-9]{7}$)/x";
  62. return (bool) preg_match($regex, $phonenumber);
  63. }
  64. /**
  65. * Formatting for Jordanian Phone Numbers.
  66. *
  67. * @param string $phonenumber
  68. * @return string Returns a string containting the phone number with some formatting.
  69. */
  70. function format_jo_phone_number($phonenumber, $field = FALSE){
  71. $phonenumber = trim($phonenumber);
  72. $regex = "/(^(\+962|00962|962|0)[-\s]{0,1}[7]{1}[7-9]{1}[0-9]{7}$) | (^(\+962|00962|962|0)[-\s]{0,1}[2-6][-\s]{0,1}[0-9]{7}$)/x";
  73. preg_match($regex, $phonenumber, $matches);
  74. $phonenumber = preg_replace('/^(\+962|00962|962|0)|[-\s]/', '', $matches[0]);
  75. return '+962'.$phonenumber;
  76. }