fork download
  1. <?php
  2.  
  3.  
  4. $pattern = "/^\\s?(8|\\+\\s?7)((-|\\s|\\(|\\))*\\d(-|\\s|\\(|\\))*){10}$/";
  5.  
  6. $allNumbers = [
  7. "correctNumbers" => [
  8. '84951234567', '+74951234567', '8-495-1-234-567',
  9. ' 8 (8122) 56-56-56', '8-911-1234567', '8 (911) 12 345 67',
  10. '8-911 12 345 67', '8 (911) - 123 - 45 - 67', '+ 7 999 123 4567',
  11. '8 ( 999 ) 1234567', '8 999 123 4567'
  12. ],
  13.  
  14. "incorrectNumbers" => [
  15. '02', '84951234567 позвать люсю', '849512345', '849512345678',
  16. '8 (409) 123-123-123', '7900123467', '5005005001', '8888-8888-88',
  17. '84951a234567', '8495123456a',
  18. '+1 234 5678901', /* неверный код страны */
  19. '+8 234 5678901', /* либо 8 либо +7 */
  20. '7 234 5678901' /* нет + */
  21. ]
  22. ];
  23.  
  24. foreach ($allNumbers as $numbers) {
  25. foreach ($numbers as $number) {
  26. if (!preg_match($pattern, $number)) {
  27. echo "{$number}\n";
  28. }
  29. }
  30. }
  31.  
Success #stdin #stdout 0.02s 52480KB
stdin
Standard input is empty
stdout
02
84951234567 позвать люсю
849512345
849512345678
8 (409) 123-123-123
7900123467
5005005001
8888-8888-88
84951a234567
8495123456a
+1 234 5678901
+8 234 5678901
7 234 5678901