Readline 函数
在线手册:中文 英文
PHP手册

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.

范例

Example #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());
?>


Readline 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Reads a line

用户评论:

Anonymous (30-May-2011 05:12)

Calling readline() always enables filename autocompletion even if not requested. It does so by looking for files in the current working directory.

This current working directory can be changed with the chdir() function.

But I don't know if there is any way to disable filename autocompletion other than set the current working directory to an empty directory.

Anonymous (30-May-2011 04:51)

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('$ ');
}
?>

sean (21-Jun-2009 10:00)

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.

taneli at crasman dot fi (24-Mar-2009 08:21)

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";
?>

rojaro at gmail dot com (31-Oct-2008 03:28)

Note that readline() will return boolean "false" when the user presses CTRL+D.

soletan at toxa dot de (04-Sep-2006 01:44)

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" );
}

haukew at gmail dot com (16-Jan-2006 01:39)

A readline for ux systems without gnu-readline. (KISS)

function readline($prompt="")
{
   print $prompt;
   $out = "";
   $key = "";
   $key = fgetc(STDIN);        //read from standard input (keyboard)
   while ($key!="\n")        //if the newline character has not yet arrived read another
   {
       $out.= $key;
       $key = fread(STDIN, 1);
   }
   return $out;
}

Vylen (27-Aug-2005 04:36)

I've got another solution for those those without readline compiled on a linux system.

This one is different as it is specifically for different PHP files opened by proc_open or in the CLI and are constantly looking for input.

<?php
// Define the pointer
$readline = popen("while true; do { read reply; echo \$reply ; }; done", "r");

// Some class
class example {
   function
data() {
      global
$readline;

     
// Return any data from $readline, substr() is to remove the trailing linebreak, its really optional.
     
return(substr(fgets($readline, 1024), 0, -1));
   }
}

$example = new example();

while(
$line = $example->data()) {
   if(
$line == 'hello')
      echo
"Hi to you too!\n";
   else
      echo
"recieved input\n";
}
?>

(12-Feb-2005 07:40)

I have my own readline function for Windows:
<?php
function readline($prompt="") {
    echo
$prompt;
   
$o = "";
   
$c = "";
    while (
$c!="\r"&&$c!="\n") {
       
$o.= $c;
       
$c = fread(STDIN, 1);
    }
   
fgetc(STDIN);
    return
$o;
}
?>
With many of the other functions posted above, it will only work once, and will immediately return while returning nothing afterwards.
----
Me;)

TheFatherMind At Dangerous-Minds.NET (03-Mar-2004 07:22)

Above I took what "christian at gaeking dot de" did and I upgraded it  a bit.  Rewrote it to my needs in the process.  I found one minor bug with what he was doing and I also improved on it....

The bug with what he had is that when you hit the keyboard "Return" key it takes that with the varible.  So if you type in 123 it takes  "123\n".  I needed to detect blank input so I purge off the "\n" on the end to fix it.

The Enhancement.  I added some options to the "read".  One is that if you optionally specify True for the second option on the ReadText function, it will NOT show what you type.  This is good for grabbing passwords.  Also I added -er to the options of "read".  "-e" allows for standard input coming from the terminal (I use this for shell scripts).  Also "-r" turns off the backslash incase you want that as part of your input.  This means you can not escape any thing while doing input.  Feel free to change the options though.  And finally I added the ability to prompt with the input.

<?php
 
Function ReadText($Prompt,$Silent=False){
   
$Options="-er";
    If(
$Silent==True){
     
$Options=$Options." -s";
    }
   
$Returned=POpen("read $Options -p \"$Prompt\"; echo \$REPLY","r");
   
$TextEntered=FGets($Returned,100);
   
PClose($Returned);
   
$TextEntered=SubStr($TextEntered,0,StrLen($TextEntered)-1-1);
    If(
$Silent==True){
      Print
"\n";
      @
OB_Flush();
     
Flush();
    }
    Return
$TextEntered;
  }
?>

christian at gaeking dot de (22-Jan-2004 06:01)

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";

Glyn AT minimanga DOT com (08-Jan-2003 08:09)

This is mentionned elsewhere on the site, but for those of you that got here by trial and error searching of function names, here's an important fact: this function is not available to windows users, but thankfully we can accomplish much the same thing.

function readline () {
$fp = fopen("php://stdin", "r");
$in = fgets($fp, 4094); // Maximum windows buffer size
fclose ($fp);

return $in;
}

$line = readline();

I appropriated this function from elsewhere on the site, but I feel it should be here to save the time of others like me. ;)

Here's a function for multi-line reading that I hacked up myself. Please forgive any obvious mistakes I make, as I am still a beginner.

function read ($stopchar) {
$fp = fopen("php://stdin", "r");
$in = fread($fp, 1); // Start the loop
$output = '';
while ($in != $stopchar)
{
    $output = $output.$in;
    $in = fread($fp, 1);
}
fclose ($fp);
return $output;
}

$lines = read(".");

This is a multi-line read function, a carriage return won't end this, as with fgets, but the $stopchar param. will. In the above example, you have to type a period followed by a carriage return to break out of the loop and get your input. The $stopchar is not included in the final output, but the last carriage return is. Both of these are simple to change to your needs.

don at undesigned dot org dot za (17-Feb-2002 05:46)

This might help some of you a bit:

/* example for how to return console colours - same as normal console colours with chr(27) for ^[ - returns 0 to reset if null */

function cl($str = 0) {
  return (chr(27) . "[" . $str . "m");
}

/* check user input for readline() (uses $default if given), asks for it again if user input is null and $default not specified. */

function readline_add($str,$default = null) {
  for ($i = 0; $i < 1; $i++) {
    if ($default !== null) {
      $line = readline(cl("1;31") . $str . cl("1;37") . " [$default]: ");
      if (isset($line) && $line !== "") {            
        readline_add_history($line); /* add history */
      } else {                                       
        readline_add_history($default); /* add default to history */
      }                                              
    } else {                                         
      $line = readline(cl("1;31") . $str . cl("1;37") . ": ");
      if (isset($line) && $line !== "") {            
        readline_add_history($line); /* add history */
      } else {                                       
        $i -= 1; /* we -1 to repeat the query if input is null */
      }                                              
    }
  }
}

readline_add("one"); /* $history[0] - if no input is given, it asks for it again */
readline_add("two","bla"); /* $history[1] - if no input is given it defaults to "bla" */

/* readline_list_history() returns an array so handle it how you wish */
$history = readline_list_history();

print "One: " . $history[0] . "\n";
print "Two: " . $history[1] . "\n";

print cl(); /* reset colours with 0 */

cox at idecnet dot com (03-Feb-2002 04:06)

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