note 28275 added to function.readline
| From: | Glyn at rack1 dot php dot net | Date: | Wed, 08 Jan 2003 06:09:56 +0000 |
| Subject: | note 28275 added to function.readline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-41812@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/function.readline.php
http://master.php.net/manage/user-notes.php?action=edit+28275
http://master.php.net/manage/user-notes.php?action=delete+28275
http://master.php.net/manage/user-notes.php?action=reject+28275