PHP: Read Specific Line From File

后端 未结 12 629
天命终不由人
天命终不由人 2020-11-28 08:15

I\'m trying to read a specific line from a text file using php. Here\'s the text file:

foo  
foo2

How would I get the content of the seco

相关标签:
12条回答
  • 2020-11-28 09:02

    you can use the following to get all the lines in the file

    $handle = @fopen('test.txt', "r");
    
    if ($handle) { 
       while (!feof($handle)) { 
           $lines[] = fgets($handle, 4096); 
       } 
       fclose($handle); 
    } 
    
    
    print_r($lines);
    

    and $lines[1] for your second line

    0 讨论(0)
  • 2020-11-28 09:04

    You have to loop the file till end of file.

      while(!feof($file))
      {
         echo fgets($file). "<br />";
      }
      fclose($file);
    
    0 讨论(0)
  • 2020-11-28 09:06

    omg I'm lacking 7 rep to make comments. This is @Raptor's & @Tomm's comment, since this question still shows up way high in google serps.

    He's exactly right. For small files file($file); is perfectly fine. For large files it's total overkill b/c php arrays eat memory like crazy.

    I just ran a tiny test with a *.csv with a file size of ~67mb (1,000,000 lines):

    $t = -microtime(1);
    $file = '../data/1000k.csv';
    $lines = file($file);
    echo $lines[999999]
        ."\n".(memory_get_peak_usage(1)/1024/1024)
        ."\n".($t+microtime(1));
    //227.5
    //0.22701287269592
    //Process finished with exit code 0
    

    And since noone mentioned it yet, I gave the SplFileObject a try, which I actually just recently discovered for myself.

    $t = -microtime(1);
    $file = '../data/1000k.csv';
    $spl = new SplFileObject($file);
    $spl->seek(999999);
    echo $spl->current()
        ."\n".(memory_get_peak_usage(1)/1024/1024)
        ."\n".($t+microtime(1));
    //0.5
    //0.11500692367554
    //Process finished with exit code 0
    

    This was on my Win7 desktop so it's not representative for production environment, but still ... quite the difference.

    0 讨论(0)
  • 2020-11-28 09:09

    You could try looping until the line you want, not the EOF, and resetting the variable to the line each time (not adding to it). In your case, the 2nd line is the EOF. (A for loop is probably more appropriate in my code below).

    This way the entire file is not in the memory; the drawback is it takes time to go through the file up to the point you want.

    <?php 
    $myFile = "4-24-11.txt";
    $fh = fopen($myFile, 'r');
    $i = 0;
    while ($i < 2)
     {
      $theData = fgets($fh);
      $i++
     }
    fclose($fh);
    echo $theData;
    ?>
    
    0 讨论(0)
  • 2020-11-28 09:11

    This question is quite old by now, but for anyone dealing with very large files, here is a solution that does not involve reading every preceding line. This was also the only solution that worked in my case for a file with ~160 million lines.

    <?php
    function rand_line($fileName) {
        do{
            $fileSize=filesize($fileName);
            $fp = fopen($fileName, 'r');
            fseek($fp, rand(0, $fileSize));
            $data = fread($fp, 4096);  // assumes lines are < 4096 characters
            fclose($fp);
            $a = explode("\n",$data);
        }while(count($a)<2);
        return $a[1];
    }
    
    echo rand_line("file.txt");  // change file name
    ?>
    

    It works by opening the file without reading anything, then moving the pointer instantly to a random position, reading up to 4096 characters from that point, then grabbing the first complete line from that data.

    0 讨论(0)
  • 2020-11-28 09:12
    $myFile = "4-24-11.txt";
    $lines = file($myFile);//file in to an array
    echo $lines[1]; //line 2
    

    file — Reads entire file into an array

    0 讨论(0)
提交回复
热议问题