PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<pspell_suggestreadline_add_history>
view the version of this page
Last updated: Sun, 02 May 2004

LXXXIX. GNU Readline

Introduzione

The readline() functions implement an interface to the GNU Readline library. These are functions that provide editable command lines. An example being the way Bash allows you to use the arrow keys to insert characters or scroll through command history. Because of the interactive nature of this library, it will be of little use for writing Web applications, but may be useful when writing scripts meant using PHP from the command line.

Nota: Questo modulo non è disponibile su piattaforme Windows.

Requisiti

To use the readline functions, you need to install libreadline. You can find libreadline on the home page of the GNU Readline project, at http://cnswww.cns.cwru.edu/~chet/readline/rltop.html. It's maintained by Chet Ramey, who's also the author of Bash.

You can also use this functions with the libedit library, a non-GPL replacement for the readline library. The libedit library is BSD licensed and available for download from http://sourceforge.net/projects/libedit/.

Installazione

To use this functions you must compile the CGI or CLI version of PHP with readline support. You need to configure PHP --with-readline[=DIR]. In order you want to use the libedit readline replacement, configure PHP --with-libedit[=DIR].

Configurazione di Runtime

Questa estensione non definisce alcuna direttiva di configurazione in php.ini

Tipi di risorse

Questa estensione non definisce alcun tipo di risorsa.

Costanti predefinite

Questa estensione non definisce alcuna costante.

Sommario
readline_add_history -- Adds a line to the history
readline_clear_history -- Clears the history
readline_completion_function -- Registers a completion function
readline_info -- Gets/sets various internal readline variables
readline_list_history -- Lists the history
readline_read_history -- Reads the history
readline_write_history -- Writes the history
readline -- Reads a line


add a note add a note User Contributed Notes
GNU Readline
ds at NOSPAM dot undesigned dot org dot za
05-Dec-2003 02:04
You can open /dev/tty on unix systems or \con in windows, with ob_implicit_flush(true) to write output unbuffered.  Works like a charm :-)

-------------------------------

#!/usr/local/bin/php -q
<?php

set_time_limit
(0);
@
ob_end_flush();
ob_implicit_flush(true);

class
prompt {
  var
$tty;

  function
prompt() {
   if (
substr(PHP_OS, 0, 3) == "WIN") {
    
$this->tty = fOpen("\con", "rb");
   } else {
     if (!(
$this->tty = fOpen("/dev/tty", "r"))) {
      
$this->tty = fOpen("php://stdin", "r");
     }
   }
  }

  function
get($string, $length = 1024) {
   echo
$string;
  
$result = trim(fGets($this->tty, $length));
   echo
"\n";
   return
$result;
  }
}

echo
"Enter something or 'exit' to quit\n";
do {
 
$cmdline = new prompt();
 
$buffer = $cmdline->get("Something: ");
  echo
"You said: $buffer\n";
} while (
$buffer !== "exit");
echo
"Goodbye\n";

?>
jewfish at jewfish dot net
10-Jun-2002 10:05
There is a simpler way to do a multiline read than above:

function multiline() {
   while(($in = readline("")) != ".")
       $story .= ($PHP_OS == "WINNT") ? "\r\n".$in :
                                         "\n".$in;

   return $story;
}
joshua at neocodesoftware.com
21-Apr-2002 09:17
Here's an example simple readline-like way to input from command line on windows - the single line is from http://www.phpbuilder.com/columns/darrell20000319.php3, the multiline is something I added...

<?
function read () {
  
# 4092 max on win32 fopen

  
$fp=fopen("php://stdin", "r");
  
$in=fgets($fp,4094);
  
fclose($fp);

  
# strip newline
  
(PHP_OS == "WINNT") ? ($read = str_replace("\r\n", "", $in)) : ($read = str_replace("\n", "", $in));

   return
$read;
}

function
multilineread () {
   do {
      
$in = read();

      
# test exit
      
if ($in == ".") return $read;

      
# concat input
      
(PHP_OS == "WINNT") ? ($read = $read . ($read ? "\r\n" : "") . $in) : ($read = $read . "\n" . $in);

   } while (
$inp != ".");

   return
$read;
}

print(
"End input with . on line by itself.\n");

print(
"What is your first name?\n");
$first_name = multilineread();

print(
"What is your last name?\n");
$last_name = read();

print(
"\nHello, $first_name $last_name! Nice to meet you! \n");
?>
14-Apr-2002 01:17
[Ed. note: you can use fopen("php://stdin", "w") to achieve the same thing, works on both Windows and Unix)]

I wanted to get console input in a PHP script running on windows, so I made a little hack, which is so simple, it is clearly public domain.  What I did was write a C++ program to get a line, then output it.  Then all that is needed is to exec() that program and capture the output - readline() for windows.  The C++ source is as follows:

#include <iostream.h>
#include <string>
void main()
{
   string input;
   cin >> input;
   cout << input;
}

It works wonderfully for my purposes, since I love the PHP language and want to have console input.

Justin Henck
twebb at boisecenter dot com
04-Mar-2002 04:11
You'll probably need the --with-readline directive when configuring php to have the readline() functions available. 

If you've installed the readline libraries in /usr/local, add:

--with-readline=/usr/local

to your ./configure command.

<pspell_suggestreadline_add_history>
 Last updated: Sun, 02 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Italia OnLine S.p.a.
Last updated: Fri May 21 04:11:23 2004 CEST