note 124837 added to function.fgets

From: Date: Sat, 21 Mar 2020 15:34:31 +0000
Subject: note 124837 added to function.fgets
Groups: php.notes 
Request: Send a blank email to php-notes+get-214349@lists.php.net to get a copy of this message
if you for some reason need to get lines from a string instead of a file pointer, try <?php function string_gets(string $source, int $offset = 0, string $delimiter = "\n"): ?string { $len = strlen($source); if ($len < $offset) { // out of bounds.. maybe i should throw an exception return null; } if ($len === $offset) { // end of string.. return null; } $delimiter_pos = strpos($source, $delimiter, $offset); if ($delimiter_pos === false) { // last line. return substr($source, $offset); } return substr($source, $offset, ($delimiter_pos - $offset) + strlen($delimiter)); } ?> (i had a ~16GB string in-memory i needed to process line-by-line, but i would get memory-allocation-crash (on a 32GB ram system) if i tried explode("\n",$str); , so came up with this.. interestingly, fgets() seems to be faster than doing it in-ram-in-php, though. php 7.3.7) ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.195 (proxied: 46.46.194.156) ---- Manual Page -- http://php.net/manual/en/function.fgets.php Edit -- https://master.php.net/note/edit/124837 Del: integrated -- https://master.php.net/note/delete/124837/integrated Del: useless -- https://master.php.net/note/delete/124837/useless Del: bad code -- https://master.php.net/note/delete/124837/bad+code Del: spam -- https://master.php.net/note/delete/124837/spam Del: non-english -- https://master.php.net/note/delete/124837/non-english Del: in docs -- https://master.php.net/note/delete/124837/in+docs Del: other reasons-- https://master.php.net/note/delete/124837 Reject -- https://master.php.net/note/reject/124837 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214349) next »