PHP: GNU Readline - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<pspell_suggestreadline_add_history>
view the version of this page
Last updated: Thu, 15 Jul 2004

XCII. GNU Readline

�vod

readline() funkce implementuj� rozhran� ke GNU Readline knihovn�. Tyto funkce poskytuj� editovateln� p��kazov� ��dky. P��kladem m��e b�t zp�sob, jak�m v�m Bash umo��uje vkl�dat znaky nebo listovat histori� p��kaz� pomoc� kl�ves pro pohyb kurzoru. Z interaktivn� povahy t�to knihovny vypl�v�, �e nen� p��li� u�ite�n� pro psan� webov�ch aplikac�, ale m��e b�t u�ite�n� p�i psan� skript�, kter� se maj� spou�t�t z p��kazov� ��dky.

Pozn�mka: Toto roz���en� nen� k dispozici na platform�ch Windows.

Po�adavky

Abyste mohli pou��vat readline funkce, mus�te nainstalovat libreadline. Domovskou str�nkou GNU Readline projektu je . Udr�uje jej Chet Ramey, kter� je tak� autorem Bashe.

Tyto funkce m��ete pou��vat tak� s knihovnou libedit, ne-GPL n�hradou za knihovnu readline. Knihovna libedit je pod licenc� BSD a st�hnout lze z .

Instalace

Abyste mohli pou��vat tyto funkce, mus�te zkompilovat CGI nebo CLI verzi PHP s podporou pro readline. PHP mus�te zkonfigurovat s volbou --with-readline[=DIR]. Pokud chcete pou��vat libedit jako n�hradu za readline, zkonfigurujte PHP s volbou --with-libedit[=DIR].

Konfigurace b�hu

Toto roz���en� nem� definov�no ��dn� konfigura�n� direktivy.

Typy prost�edk�

Toto roz���en� nem� definov�n ��dn� typ prost�edku (resource).

P�eddefinovan� konstanty

Toto roz���en� nem� definov�ny ��dn� konstanty.

Obsah
readline_add_history -- P�idat ��dek do historie
readline_clear_history -- Smazat historii
readline_completion_function -- Zaregistrovat dokon�uj�c� funkci
readline_info -- Zjistit/nastavit r�zn� intern� prom�nn� readline
readline_list_history -- Vypsat historii
readline_read_history -- Vr�tit historii
readline_write_history -- Zapsat historii
readline -- P�e��st ��dek


add a note add a note User Contributed Notes
GNU Readline
plz at dont dot spam
08-Aug-2004 08:50
To get all arguments passed to a batch file in one variable
rather than using %1 %2 %3 etc;

:LOOP
if "%1" == "" goto DONE
set args=%args% %1
shift
goto LOOP
:DONE
@c:\\php\\cli\\php.exe script.php %args%
set args=
ds at NOSPAM dot undesigned dot org dot za
05-Dec-2003 04: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 11: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 10:17
Here's an example simple readline-like way to input from command line on windows - the single line is from 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 02: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 06: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: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Sun Nov 14 23:09:54 2004 Local time zone must be set--see zic manual page