(PHP 4, PHP 5)
readline — Liest eine Zeile
Liest eine einzelne Zeile vom Benutzer ein. Sie müssen diese Zeile selbst mittels readline_add_history() zur History hinzufügen.
Sie können eine Zeichenkette angeben, mit dem Sie den Benutzer zur Eingabe auffordern.
Gibt eine einzelne Zeile des Benutzers zurück. Der Zeile, die Sie erhalten, fehlt das abschließende Zeichen für einen Zeilenvorschub.
Beispiel #1 readline()-Beispiel
<?php
// Drei Kommandos vom Benutzer abfragen
for ($i=0; $i < 3; $i++) {
$line = readline("Command: ");
readline_add_history($line);
}
// History ausgeben
print_r(readline_list_history());
// Variablen ausgeben
print_r(readline_info());
?>
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.
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";
?>
Note that readline() will return boolean "false" when the user presses CTRL+D.
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" );
}
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;
}
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";
}
?>
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;)
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;
}
?>
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";
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.
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 */
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