readline

(PHP 4, PHP 5)

readlineReads a line

Описание

string readline ([ string $prompt ] )

Reads a single line from the user. You must add this line to the history yourself using readline_add_history().

Список параметров

prompt

You may specify a string with which to prompt the user.

Возвращаемые значения

Returns a single string from the user. The line returned has the ending newline removed.

Примеры

Пример #1 readline() Example

<?php
//get 3 commands from user
for ($i=0$i 3$i++) {
        
$line readline("Command: ");
        
readline_add_history($line);
}

//dump history
print_r(readline_list_history());

//dump variables
print_r(readline_info());
?>

Коментарии

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
2002-02-03 10:06:39
http://php5.kiev.ua/manual/ru/function.readline.html
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";
2004-01-22 12:01:53
http://php5.kiev.ua/manual/ru/function.readline.html
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" );
}
2006-09-04 08:44:17
http://php5.kiev.ua/manual/ru/function.readline.html
Note that readline() will return boolean "false" when the user presses CTRL+D.
2008-10-31 09:28:41
http://php5.kiev.ua/manual/ru/function.readline.html
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$i1));
   
readline_callback_read_char();
  }

 
$prompt_finished FALSE;
 
$prompt_answer FALSE;
  while (!
$prompt_finished)
   
readline_callback_read_char();
  echo 
'You wrote: ' $prompt_answer "\n";
?>
2009-03-24 02:21:10
http://php5.kiev.ua/manual/ru/function.readline.html
Автор:
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.
2009-06-21 05:00:53
http://php5.kiev.ua/manual/ru/function.readline.html
Автор:
The readline library is not available on Windows.

<?php
if (PHP_OS == 'WINNT') {
  echo 
'$ ';
 
$line stream_get_line(STDIN1024PHP_EOL);
} else {
 
$line readline('$ ');
}
?>
2011-05-30 11:51:08
http://php5.kiev.ua/manual/ru/function.readline.html
<?php

class ConsoleQuestion
{

    function 
readline()
    {
        return 
rtrim(fgets(STDIN));
    }
}

//Example1
$line = new ConsoleQuestion();
$prompt "What Is Your Name: ";
echo 
$prompt;
$answer $line->readline();
echo 
"You Entered: " $answer;

//Example2 (comment Ex1 then uncomment Ex2)
/*$prompt = "What Is Your Name: ";
echo $prompt;
$answer =  "You Entered: " . rtrim( fgets( STDIN ));
echo $answer;*/

?>
2016-03-07 17:57:51
http://php5.kiev.ua/manual/ru/function.readline.html
Автор:
If your CLI script accepts input from STDIN and you also want it to prompt for a password (e.g. as mysql client does), then readline() won't work for you. 
What you need to do is read from the terminal device as shown below.

function readline_terminal($prompt = '') {
    $prompt && print $prompt;
    $terminal_device = '/dev/tty';
    $h = fopen($terminal_device, 'r');
    if ($h === false) {
        #throw new RuntimeException("Failed to open terminal device $terminal_device");
        return false; # probably not running in a terminal.
    }
    $line = rtrim(fgets($h),"\r\n");
    fclose($h);
    return $line;
}
$pass = readline_terminal('Password: ');
2017-03-03 02:07:21
http://php5.kiev.ua/manual/ru/function.readline.html
Автор:
a few observations....

I use Cygwin PHP v7 and readline is available. The readline_list_history() function though is not defined.

A prompt with escape sequences are sanitized, so use something like:

<?php
   
echo("\e[0m\e[34mPromt>\e[0m");
   
$inp readline(' ');
?>

I have not fully documented it, but I see that sometimes strings beginning with punctuation characters do not make it into the history with readline_add_history(). They also sometimes clear the prompt string.
2017-06-18 22:41:31
http://php5.kiev.ua/manual/ru/function.readline.html

    Поддержать сайт на родительском проекте КГБ