PHP: Read Specific Line From File

后端 未结 12 651
天命终不由人
天命终不由人 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 08:55

    If you wanted to do it that way...

    $line = 0;
    
    while (($buffer = fgets($fh)) !== FALSE) {
       if ($line == 1) {
           // This is the second line.
           break;
       }   
       $line++;
    }
    

    Alternatively, open it with file() and subscript the line with [1].

提交回复
热议问题