note 126526 added to splfileobject.fgets
| From: | LucasBustamante at main dot php dot net | Date: | Fri, 22 Oct 2021 11:34:44 +0000 |
| Subject: | note 126526 added to splfileobject.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217099@lists.php.net to get a copy of this message | ||
I forgot to mention in my previous note about PHP PHP 8.0.10, that you can use $file->current();
$file->next(); as a replacement for $file->fgets(); that works consistently from PHP 5.1 to
8.0.1+ after a seek():
<?php
$file = new SplTempFileObject();
for ($i = 0; $i < 100; $i++) {
$file->fwrite("Foo $i\n");
}
$file->seek(50);
print_r(array(
array('line' => $file->key(), 'contents' =>
trim($file->current()), 'triggerNext' => $file->next()),
array('line' => $file->key(), 'contents' =>
trim($file->current()), 'triggerNext' => $file->next()),
array('line' => $file->key(), 'contents' =>
trim($file->current()), 'triggerNext' => $file->next()),
));
?>
PHP 5.1 to 8.0.1+:
[
{
"line": 50,
"contents": "Foo 50"
},
{
"line": 51,
"contents": "Foo 51"
},
{
"line": 52,
"contents": "Foo 52"
}
]
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 2804:584:a1fc:d501:1e82:66d5:d851:ed4d)
----
Manual Page -- https://php.net/manual/en/splfileobject.fgets.php
Edit -- https://main.php.net/note/edit/126526
Del: integrated -- https://main.php.net/note/delete/126526/integrated
Del: useless -- https://main.php.net/note/delete/126526/useless
Del: bad code -- https://main.php.net/note/delete/126526/bad+code
Del: spam -- https://main.php.net/note/delete/126526/spam
Del: non-english -- https://main.php.net/note/delete/126526/non-english
Del: in docs -- https://main.php.net/note/delete/126526/in+docs
Del: other reasons-- https://main.php.net/note/delete/126526
Reject -- https://main.php.net/note/reject/126526
Search -- https://main.php.net/manage/user-notes.php