note 27129 deleted from function.fopen by alindeman
| From: | alindeman@php.net | Date: | Sat, 17 May 2003 21:07:16 +0000 |
| Subject: | note 27129 deleted from function.fopen by alindeman | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-48264@lists.php.net to get a copy of this message | ||
Note Submitter: mackstann@in[REMOVE_THIS]cise.org
i was wanting to use fopen('php://stdin' ....) to get a block of text in the same way that
stdin works on the console, that is, you can keep hitting enter, and it will keep taking text, until
you hit enter for a new line and then send EOF (usually ^D). so i wrote this little class real
quick which gets single lines, and also gets blocks of text, from the command line.
<?php
class GetStdin {
function getLine() {
$fp = fopen("php://stdin", "r");
if(feof($fp))
$input = FALSE;
else
$input = fgets($fp, 4096);
fclose($fp);
return $input;
}
function getBlock() {
while(($line = $this->getLine()) !== FALSE)
$block .= $line;
return $block;
}
}
?>
and a simple little demo:
<?php
$textfromconsole = new GetStdin;
echo $textfromconsole->getLine();
echo "----\n";
echo $textfromconsole->getBlock();
?>
hope someone finds this useful.