2017-03-22 2 views
0

PHP fgets (STDIN)를 만들고 싶습니다. 그러나 입력 횟수를 제한하는 방법을 모르며 특정 문자 만 받아들이는 방법을 모르겠습니다. 나를 안내 해줘. 나는 내 문제를 적어 둔다. 나는 STDIN 사용의 초보자이다. 그것은 내가 프로그래밍 시험 시험에서해야하기 때문에 중요합니다. 도와주세요, 제발. 여기PHP STDIN에서 문자 수를 제한하고 특정 문자 만 허용하는 방법은 무엇입니까?

는 문제 1

문제 1 : I cmd를 입력에서 10 문자를 얻고 싶은 이들 입력되어야 & S. 여기

W 문제 2

문제 2 : 3 개의 숫자 만 입력하고 cmd 입력을 숫자로 제한하는 방법을 원합니다.

답변

0

난 그냥 필자가 2 부에 대한 질문을 오해 실현,하지만이 부분을 하나의 작동 및 문제 2 시작하기에 좋은 도움이 될 것입니다 :

<?php 
    $errorMessage = false; 
    $attempts = 1; 
    $maxAttempts = 5; 

    function validates($string) 
    { 
     global $errorMessage; //tell this function to use global errormessage 
     if (strlen($string) > 10) { 
      $errorMessage = 'Exceeded String Length. Please ensure no more than 10 characters are entered'; 
      return false; 
     } 
     preg_match('/\d+/', $string, $matches); 
     $extractedDigits = (isset($matches[0]) ? $matches[0] : 0); 
     if ($extractedDigits > 3) { 
      $errorMessage = 'Exceeded allowed number of digits. Please ensure no more than 3 digits are contained in the string'; 
      return false; 
     } 
     $errorMessage = false; 
     return true; 
    } 
    $result = readline('please enter a valid string: '); 
    while (!validates($result) && $attempts < $maxAttempts) { //while fail validation and have attempted less than the max attampts 
     echo "\n"; 
     echo '*******************************************'; 
     echo "\n"; 
     echo 'Attempts Left (' . ($maxAttempts - $attempts) . ')'; 
     echo "\n"; 
     $result = readline($errorMessage . '. Please try again: '); 
     $attempts++; 
    } 
    echo "\n"; 
    echo '*******************************************'; 
    echo "\n"; 
    if ($attempts >= $maxAttempts) { 
     echo 'Failed, too many attempts'; 
    } else { 
     echo "\n"; 
     echo 'Success...'; 
     echo "\n"; 
     echo "\n"; 
     echo $result; 
     echo "\n"; 
     echo "\n"; 
    } 
    echo "\n"; 
+0

이 부분이 들어 그냥 확인을 조정할 필요를 기능 –

관련 문제