readline

(PHP 4, PHP 5)

readline -- Reads a line

Description

string readline ( string prompt )

This function returns a single string from the user. You may specify a string with which to prompt the user. The line returned has the ending newline removed. You must add this line to the history yourself using readline_add_history().

例子 1. readline()

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

add a note add a note User Contributed Notes
haukew at gmail dot com
16-Jan-2006 09: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 11: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";
}
?>
13-Feb-2005 03: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 03: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
23-Jan-2004 02: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 04: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 01: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
04-Feb-2002 12: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