fork download
  1. <?php
  2.  
  3. function checkN($string){
  4. $reg='/^((\+\s?7\s?)|(\s?8(\W)?))((\W+)?\d(\W)?){10}$/';
  5. if(preg_match($reg,$string)){return true;}
  6. else {return false;}
  7.  
  8. }
  9.  
  10. //tests
  11.  
  12. $testArray=['84951234567','+74951234567','8-495-1-234-567','8 (8122) 56-56-56','
  13. 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',
  14. '8 999 123 4567'];
  15. $falseArray=['02','84951234567 позвать люсю','849512345','849512345678','8 (409) 123-123-123','7900123467','5005005001',
  16. '8888-8888-88','84951a234567','8495123456a','+1 234 5678901','+8 234 5678901','7 234 5678901'];
  17.  
  18. function tester($arr){$matches=0;$mistakes=[];for ($i=0;$i<count($arr);$i++){if(checkN($arr[$i])) {$matches++;}else{array_push($arr[$i],$mistakes);}}return $matches;}
  19. if (tester($testArray)==count($testArray)) {echo "Positive tests passed\n";} else {echo "{$mistakes}";}
  20. if (tester($falseArray)==0){echo "Negative tests passed\n";} else {echo "Only these passed negative tests {$mistakes}\n";}
  21.  
  22. //tests passed can work with the function further
  23.  
  24. function replacer($string2){$result=NULL;
  25. if (checkN($string2)){
  26. $regexp='/[() -]*/';
  27. $re='/^(\+7)/';
  28. $result=preg_replace($regexp,'',$string2);
  29. $result=preg_replace('/^(\+7)/','8',$result);
  30. echo "$result";
  31. }
  32. else {echo "Not a number";}
  33. }
  34. replacer('+7(917)99999999');
  35. ?>
Success #stdin #stdout #stderr 0.02s 82624KB
stdin
Standard input is empty
stdout
Positive tests passed
Negative tests passed
Not a number
stderr
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, string given in /home/A2VmLC/prog.php on line 19