fork download
  1. <?php
  2. $lines = array(
  3. '84951234567',
  4. '+74951234567',
  5. '8-495-1-234-567',
  6. '8 (8122) 56-56-56',
  7. ' 8-911-1234567',
  8. '8 (911) 12 345 67',
  9. '8-911 12 345 67',
  10. '8 (911) - 123 - 45 - 67',
  11. '+ 7 999 123 4567',
  12. '8 ( 909 ) 1234567',
  13. '8 999 123 4567',
  14. '8 952-58 -51 25-5',
  15. '02',
  16. '84951234567 позвать люсю',
  17. '849512345',
  18. '849512345678',
  19. '8 (409) 123-123-123',
  20. '7900123467',
  21. '5005005001',
  22. '8888-8888-88',
  23. '84951a234567',
  24. '8495123456a',
  25. '+1 234 5678901',
  26. '+8 234 5678901',
  27. '7 234 5678901'
  28. );
  29.  
  30. foreach($lines as $key => $phoneNumber) {
  31. if($number = preg_replace(["/^\\s*\\+\\s*7/", "/[\\(\\)\\-\\s]/", "/^[^8].*/", "/.*?[^\d].*/", "/^[0-9]{12,}$/", "/^[0-9]{1,10}$/"], ["8", ""], $phoneNumber)) {
  32. echo "#".str_pad($key, 2)." phone number: ".$number.PHP_EOL;
  33. } else {
  34. echo "#".str_pad($key, 2)." wrong format!".PHP_EOL;
  35. }
  36. }
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
#0  phone number: 84951234567
#1  phone number: 84951234567
#2  phone number: 84951234567
#3  phone number: 88122565656
#4  phone number: 89111234567
#5  phone number: 89111234567
#6  phone number: 89111234567
#7  phone number: 89111234567
#8  phone number: 89991234567
#9  phone number: 89091234567
#10 phone number: 89991234567
#11 phone number: 89525851255
#12 wrong format!
#13 wrong format!
#14 wrong format!
#15 wrong format!
#16 wrong format!
#17 wrong format!
#18 wrong format!
#19 wrong format!
#20 wrong format!
#21 wrong format!
#22 wrong format!
#23 wrong format!
#24 wrong format!