Voting

: min(two, six)?
(Example: nine)

The Note You're Voting On

Anonymous
5 years ago
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)

<< Back to user notes page

To Top