Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created October 6, 2016 17:20
Show Gist options
  • Save sasezaki/1f2815127c6786604e3d577843684315 to your computer and use it in GitHub Desktop.
Save sasezaki/1f2815127c6786604e3d577843684315 to your computer and use it in GitHub Desktop.
(not well) stream_get_line_reverses
<?php
$tmp = tmpfile();
$fp = fopen('stream.php', 'r');
//$fp = fopen('streamCRLF.php', 'r');
//$fp = fopen('streamCR.php', 'r');
function fpulls($fp, &$i) {
$line = '';
while (true) {
fseek($fp, $i, SEEK_END);
if (feof($fp)) {
$i = false;
break;
}
--$i;
$char = fgetc($fp);
if ($char === false) {
break;
}
// LF?
if (chr(10) === $char) {
fseek($fp , $i - 1 , SEEK_END);
if (chr(12) === fgetc($fp)) {
--$i;
}
break;
}
if (chr(12) === $char ) {
break;
}
$line = $char . $line;
}
return $line;
}
$i = 0;
do {
$line = fpulls($fp, $i);
fwrite($tmp, ($i !== false) ? $line . "\n" : $line);
var_dump($line);
} while ($i !== false);
rewind($tmp);
var_dump(stream_get_contents($tmp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment