note 27129 added to function.fopen

From: Date: Sun, 24 Nov 2002 08:53:11 +0000
Subject: note 27129 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-39907@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/function.fopen.php http://master.php.net/manage/user-notes.php?action=edit+27129 http://master.php.net/manage/user-notes.php?action=delete+27129 http://master.php.net/manage/user-notes.php?action=reject+27129

« previous php.notes (#39907) next »