fork download
  1. <?php
  2.  
  3. header("Content-Type: text/plain; charset=utf-8");
  4.  
  5. /**
  6.  * (\s?\+\s?7\s?|\s?8\s?) - +7 или 8 (с любым кол-вом пробелов между ними)
  7.  * (\s*\(*-*\s*\d\s*-*\)*\s*){10} - Пробел-Скобочка-Тире-Пробел-ЦИФРА-Пробел-Тире-Скобочка-Пробел
  8.  */
  9.  
  10. $regexp = '!^(\s?\+\s?7\s?|\s?8\s?)(\s*\(*-*\s*\d\s*-*\)*\s*){10}$!';
  11.  
  12. $correctNumber = array(
  13. '84951234567',
  14. '+74951234567',
  15. ' +74957654321',
  16. '8-495-1-234-567',
  17. ' 8 (8122) 56-56-56',
  18. '8-911-1234567',
  19. '8 (911) 12 345 67',
  20. '8-911 12 345 67',
  21. '8 (911) - 123 - 45 - 67',
  22. '+ 7 999 123 4567',
  23. '8 ( 999 ) 1234567',
  24. '8 999 123 4567'
  25. );
  26.  
  27. $wrongNumber = array(
  28. '02',
  29. '84951234567 позвать люсю',
  30. '849512345',
  31. '849512345678',
  32. '8 (409) 123-123-123',
  33. '7900123467',
  34. '5005005001',
  35. '8888-8888-88',
  36. '8888-8888-888888888888888888888888888888888888888888888888888888',
  37. '84951a234567',
  38. '8495123456a',
  39. '+1 234 5678901',
  40. '+8 234 5678901',
  41. '7 234 5678901'
  42. );
  43.  
  44. echo "TRUE\n\n";
  45.  
  46. foreach ($correctNumber as $number) {
  47. if (preg_match($regexp, $number)) {
  48. echo "[+] \"{$number}\"\n";
  49. } else {
  50. echo "[-] \"{$number}\"\n";
  51. }
  52. }
  53.  
  54. echo "\nFALSE\n\n";
  55.  
  56. foreach ($wrongNumber as $number) {
  57. if (preg_match($regexp, $number)) {
  58. echo "[+] \"{$number}\"\n";
  59. } else {
  60. echo "[-] \"{$number}\"\n";
  61. }
  62. }
  63.  
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
TRUE

[+] "84951234567"
[+] "+74951234567"
[+] " +74957654321"
[+] "8-495-1-234-567"
[+] " 8 (8122) 56-56-56"
[+] "8-911-1234567"
[+] "8 (911) 12 345 67"
[+] "8-911 12 345 67"
[+] "8 (911) - 123 - 45 - 67"
[+] "+ 7 999 123 4567"
[+] "8 ( 999 ) 1234567"
[+] "8 999 123 4567"

FALSE

[-] "02"
[-] "84951234567 позвать люсю"
[-] "849512345"
[-] "849512345678"
[-] "8 (409) 123-123-123"
[-] "7900123467"
[-] "5005005001"
[-] "8888-8888-88"
[-] "8888-8888-888888888888888888888888888888888888888888888888888888"
[-] "84951a234567"
[-] "8495123456a"
[-] "+1 234 5678901"
[-] "+8 234 5678901"
[-] "7 234 5678901"