fork(3) download
  1. <?php
  2. $regExp = "/^\\s*(([+]\\s*7)|8)(\W*[0-9]\W*){10}$/ui";
  3. $correctNumbers = [
  4. '84951234567', '+74951234567', '8-495-1-234-567',
  5. ' 8 (8122) 56-56-56', '8-911-1234567', '8 (911) 12 345 67',
  6. '8-911 12 345 67', '8 (911) - 123 - 45 - 67', '+ 7 999 123 4567',
  7. '8 ( 999 ) 1234567', '8 999 123 4567'
  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.  
  19. foreach ($correctNumbers as $correctNumber){
  20. if (preg_match($regExp, $correctNumber)){
  21. echo "the number is right: $correctNumber\n";
  22. } else{
  23. echo "Sike that's the wrong number: $correctNumber \n";
  24. }
  25. }
  26.  
  27. foreach ($incorrectNumbers as $incorrectNumber){
  28. if(preg_match($regExp, $incorrectNumber) == 0){
  29. echo "Wrong: $incorrectNumber\n";
  30. } else{
  31. echo "Right: $incorrectNumber\n";
  32. }
  33. }
Success #stdin #stdout 0.04s 82944KB
stdin
Standard input is empty
stdout
the number is right: 84951234567
the number is right: +74951234567
the number is right: 8-495-1-234-567
the number is right:  8 (8122) 56-56-56
the number is right: 8-911-1234567
the number is right: 8 (911) 12 345 67
the number is right: 8-911 12 345 67
the number is right: 8 (911) - 123 - 45 - 67
the number is right: + 7 999 123 4567
the number is right: 8 ( 999 ) 1234567
the number is right: 8 999 123 4567
Wrong: 02
Wrong: 84951234567 позвать люсю
Wrong: 849512345
Wrong: 849512345678
Wrong: 8 (409) 123-123-123
Wrong: 7900123467
Wrong: 5005005001
Wrong: 8888-8888-88
Wrong: 84951a234567
Wrong: 8495123456a
Wrong: +1 234 5678901
Wrong: +8 234 5678901
Wrong: 7 234 5678901