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) {
return null;
}
if ($len === $offset) {
return null;
}
$delimiter_pos = strpos($source, $delimiter, $offset);
if ($delimiter_pos === false) {
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)