PHP: Read Specific Line From File

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

    I like daggett answer but there is another solution you can get try if your file is not big enough.

    $file = __FILE__; // Let's take the current file just as an example.
    
    $start_line = __LINE__ -1; // The same with the line what we look for. Take the line number where $line variable is declared as the start.
    
    $lines_to_display = 5; // The number of lines to display. Displays only the $start_line if set to 1. If $lines_to_display argument is omitted displays all lines starting from the $start_line.
    
    echo implode('', array_slice(file($file), $start_line, lines_to_display));
    

提交回复
热议问题