ever like this:
Type your answer here [ ] thanks!
so user's terminal cursor goes after [ and users answer will starts typing from there!
it can be helpful if you wanna set answer length!
following function can help you!
function posReadline ($prompt=null, $len = 1, $force = false) {
$pos = strpos($prompt, '%s');
if($pos !== false) $prompt = sprintf($prompt, '[' . str_repeat(' ', $len) . ']');
$back = $pos ? strlen($prompt) - $pos - 1 : -1;
prompt:
echo $prompt . "\033[".$back."D";
while (($res = fgets(STDIN)) === false) {
usleep(10);
}
$res = str_replace(PHP_EOL, null, $res);
if ($force && strlen($res) !== $len) goto prompt;
return $res;
}
you can using like this:
$input = posReadline('Insert your answer: %s ,Thanks!', 6, true);
if you set $force to true (just like the example) user must enter an answer with the given asked length!
Thanks
Обсуждают сегодня