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


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