Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: readline - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

圧縮およびアーカイブ" width="11" height="7"/> <Readline 関数
Last updated: Fri, 04 Jul 2008

view this page in

readline

(PHP 4, PHP 5)

readline — 一行読み込む

説明

string readline ([ string $prompt ] )

ユーザからの入力を一行読み込みます。 この行を readline_add_history(). を用いてヒストリに追加する必要があります。

パラメータ

prompt

ユーザに示す確認文字列を指定します。

返り値

ユーザから取得した文字列を一つだけ返します。 返り値の最後の改行は取り除かれます。

例1 readline() の例

<?php
// ユーザから 3 回コマンドを取得
for ($i=0$i 3$i++) {
        
$line readline("Command: ");
        
readline_add_history($line);
}

// ヒストリをダンプ
print_r(readline_list_history());

// 変数をダンプ
print_r(readline_info());
?>



add a note add a note User Contributed Notes
readline
soletan at toxa dot de
04-Sep-2006 09:44
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" );
}
haukew at gmail dot com
16-Jan-2006 10: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
28-Aug-2005 12: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 04: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 04: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 03: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 05: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 02: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 01: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

 
show source | credits | sitemap | contact | advertising | mirror sites