in reply to Re: Efficienty truncating a long string
in thread Efficienty truncating a long string

Then you probably want to use 4-arg substr to truncate the buffer and save the fragment all at once:
$end = rindex($buffer, "\n"); if ($end < 0) { whoops, incomplete line } $fragment = substr($buffer, $end+1, length($buffer), '');
Note that length($buffer) is bigger than the actual length(buffer)-$end-1 you want, but specifying a length larger than is available is guaranteed to not cause problems; you'll just get what is there. If the buffer ends with "\n", you'll get an empty $fragment.