开发者

How to read big file by 100 lines?

I have a rather big .txt file (~22开发者_运维知识库0Mb) and I need to read it by 100 lines (\n symbol) chunks (for example). How can I do it using php?

Thank you.


fopen and fgets. The fgets manual page has an example on reading a file line-by-line without loading it into memory all at once.


$fp = open('big_text_file.txt',"r");
if($fp){
    $c = 0;
    $data = array();
    while(!feof($fp)){
        if($c == 100){
            $c = 0;
            // Do whatever it is you want here
            unset($data);
            $data = array();
        }
        $data[] = fgets($fp,4096);        
        $c++;
    }
    if($c > 0){
        // Do whatever you need to again
    }
    fclose($fp);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜