I want to make php fgets(STDIN). But I don't know how to restrict the input count and i don't know how to accept only specific letters. Please guide me. I write down the problems of mine. I'm the beginner in STDIN usage. It's important for me because i have to do it in my Programming Test Exam. Help me please.
Here is Problem 1
Problem 1: I want to get only 10 Letters from cmd input and these input should be W & S.
Here is Problem 2
Problem 2 : I want to get only 3 numbers and how to restrict the cmd input to only number.
I just realized Ive misread your question for part 2, but this will work for part one and be a good help to start problem 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";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.