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