Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : GNU Readline : readline

readline

Reads a line (PHP 4, PHP 5)
string readline ( [string prompt] )

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

Code Examples / Notes » readline

soletan

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


don

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 */


glyn

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.


cox

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


vylen

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

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

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


haukew

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


Change Language


Follow Navioo On Twitter
readline_add_history
readline_callback_handler_install
readline_callback_handler_remove
readline_callback_read_char
readline_clear_history
readline_completion_function
readline_info
readline_list_history
readline_on_new_line
readline_read_history
readline_redisplay
readline_write_history
readline
eXTReMe Tracker