note 70267 added to function.fread

From: Date: Mon, 09 Oct 2006 14:03:46 +0000
Subject: note 70267 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-118316@lists.php.net to get a copy of this message
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. ---- Server IP: 193.27.201.10 (proxied: 193.27.201.10 1.0 www1.shellkonto.se:3128 (squid/2.5.STABLE4)) Probable Submitter: 80.217.189.168 ---- Manual Page -- http://www.php.net/manual/en/function.fread.php Edit -- https://master.php.net/note/edit/70267 Del: integrated -- https://master.php.net/note/delete/70267/integrated Del: useless -- https://master.php.net/note/delete/70267/useless Del: bad code -- https://master.php.net/note/delete/70267/bad+code Del: spam -- https://master.php.net/note/delete/70267/spam Del: non-english -- https://master.php.net/note/delete/70267/non-english Del: in docs -- https://master.php.net/note/delete/70267/in+docs Del: other reasons-- https://master.php.net/note/delete/70267 Reject -- https://master.php.net/note/reject/70267 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118316) next »