fork(1) download
  1. <?php
  2.  
  3. // телефонный номер
  4. //регулярка https://r...content-available-to-author-only...1.com/r/qF7vT8/3
  5.  
  6. $regExp = "/^(\+ *7| *8)( *\(*\)* *-* *\d){10}$/m";
  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. foreach($correctNumbers as $number){
  24. if (preg_match($regExp, $number))
  25. echo "{$number}\n";
  26. }
  27.  
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
84951234567
+74951234567
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