Note that readline() will return boolean "false" when the user presses CTRL+D.
readline
(PHP 4, PHP 5)
readline — Liest eine Zeile
Beschreibung
string readline
([ string
$prompt
] )Liest eine einzelne Zeile vom Benutzer ein. Sie müssen diese Zeile selbst mittels readline_add_history() zur History hinzufügen.
Parameter-Liste
-
prompt -
Sie können eine Zeichenkette angeben, mit dem Sie den Benutzer zur Eingabe auffordern.
Rückgabewerte
Gibt eine einzelne Zeile des Benutzers zurück. Der Zeile, die Sie erhalten, fehlt das abschließende Zeichen für einen Zeilenvorschub.
Beispiele
Beispiel #1 readline()-Beispiel
<?php
// Drei Kommandos vom Benutzer abfragen
for ($i=0; $i < 3; $i++) {
$line = readline("Command: ");
readline_add_history($line);
}
// History ausgeben
print_r(readline_list_history());
// Variablen ausgeben
print_r(readline_info());
?>
rojaro at gmail dot com ¶
4 years ago
sean ¶
3 years ago
I wanted a function that would timeout if readline was waiting too long... this works on php CLI on linux:
<?php
function readline_timeout($sec, $def)
{
return trim(shell_exec('bash -c ' .
escapeshellarg('phprlto=' .
escapeshellarg($def) . ';' .
'read -t ' . ((int)$sec) . ' phprlto;' .
'echo "$phprlto"')));
}
?>
Just call readline_timeout(5, 'whatever') to either read something from stdin, or timeout in 5 seconds and default to 'whatever'. I tried just using shell_exec without relying on bash -c, but that didn't work for me, so I had to go the round about way.
Anonymous ¶
1 year ago
The readline library is not available on Windows.
<?php
if (PHP_OS == 'WINNT') {
echo '$ ';
$line = stream_get_line(STDIN, 1024, PHP_EOL);
} else {
$line = readline('$ ');
}
?>
taneli at crasman dot fi ¶
4 years ago
If you want to prefill the prompt with something when using readline, this worked for me:
<?php
function readline_callback($ret)
{
global $prompt_answer, $prompt_finished;
$prompt_answer = $ret;
$prompt_finished = TRUE;
readline_callback_handler_remove();
}
readline_callback_handler_install('Enter some text> ',
'readline_callback');
$prefill = 'foobar';
for ($i = 0; $i < strlen($prefill); $i++)
{
readline_info('pending_input', substr($prefill, $i, 1));
readline_callback_read_char();
}
$prompt_finished = FALSE;
$prompt_answer = FALSE;
while (!$prompt_finished)
readline_callback_read_char();
echo 'You wrote: ' . $prompt_answer . "\n";
?>
soletan at toxa dot de ¶
6 years ago
To haukew at gmail dot com:
readline provides more features than reading a single line of input ... your example misses line editing and history. If you don't need that, use something as simple as this:
function readline( $prompt = '' )
{
echo $prompt;
return rtrim( fgets( STDIN ), "\n" );
}
christian at gaeking dot de ¶
9 years ago
A workaround if readline is not compiled into php, because for example the command is only needed within an installation routine. It works as follows under Linux:
$f=popen("read; echo \$REPLY","r");
$input=fgets($f,100);
pclose($f);
echo "Entered: $input\n";
cox at idecnet dot com ¶
11 years ago
In CGI mode be sure to call:
ob_implicit_flush(true);
at the top of your script if you want to be able to output data before and after the prompt.
-- Tomas V.V.Cox
Anonymous ¶
1 year ago
Calling readline() always enables filename autocompletion even if not requested. It does so by looking for files in the current working directory.
EDIT: if you don't want/need this then register your own completion function using http://php.net/manual/en/readline-completion-function
