Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to read large .gz file line by line. Here is what i got so far:

$sfp = gzopen($filename, "rb");
while (!gzeof($sfp)) 
{
    $line = gzread($sfp, 4096);
}

and thats where the problem comes in, gzread reading the length specified in variable (in this case 4096) and ignoring new lines .

I checked "fget" function and it works properly, so its reading line limiting the size by hitting new line or the size which ever comes first. How I can do the same with gzread or any work around ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use fgets() just like reading an ordinary file.

You shouldn't use b mode if you're reading lines, since line-by-line implies it's a text file.

$sfp = gzopen($filename, "r");
while ($line = fgets($sfp)) {
    echo $line;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...