note 70267 deleted from function.fread by nlopess
| From: | nlopess@php.net | Date: | Wed, 08 Nov 2006 22:54:09 +0000 |
| Subject: | note 70267 deleted from function.fread by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119525@lists.php.net to get a copy of this message | ||
Note Submitter: ludvig dot ericson at gmail dot com
----
When I were making command-line interfacing PHP scripts, the below post wouldn't work. The
problem is that php://stdin could only be opened once, so I had this:
<?php
function read_input($maxchars = false) {
static $fh;
$fh = (!is_resource($fh)) ? fopen('php://stdin') : $fh;
if ($maxchars !== false) {
return fgets($fh, (int)$maxchars);
} else {
return fgets($fh);
}
}
?>
This way, you can do something like <?php $input = read_input(); ?> and it'll be set to
exactly one line of what the terminal inputs, since fgets() will return when EOL is reached, and
with $maxchars you're able to limit it to something reasonable like 255 or so, still breaking
on EOL.