Home >Backend Development >PHP Tutorial >PHP reads a certain line of data from a large file text TEXT

PHP reads a certain line of data from a large file text TEXT

高洛峰
高洛峰Original
2016-11-28 16:43:211714browse

function get_line($file,$line) {
    $fp = fopen($file,'r');
    $i = 0;
    while(!feof($fp)) {
        $i++;
        $c = fgets($fp);
        if($i==$line) {
            echo $c;
            break;
        }
    }
}

The efficiency of this method still needs to be tested

In an apache log file (450M)

Reading the 20th line is very fast 0.001039981842041

Reading the 200th line 0.0033199787139893

It takes 0.30908 to read the 20,000th line 799171448 seconds About

Reading the 200,000th row 2.5622429847717 seconds has reached the point of being unbearable


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn