Reputation: 1
G'day everyone, I have a .txt file and want to get line X from it. Something like:
"list.txt":
Line1
Line2
Line3
"file.php":
$line = 1;
echo "getLine("list.txt", $line)";
Basicly I want it to echo "Line2". Any ideas what function I can use in "file.php"?
Upvotes: 0
Views: 1125
Reputation: 33823
Reading a file with file
creates an array so you should be able to access a line by index number
$file='list.txt';
$lines=file( $file );
echo $lines[1], $lines[2], $lines[3];
Upvotes: 0
Reputation: 4207
You may use SplFileObject
, see example below:
$file = new SplFileObject('example.txt');
$file->seek(1);
echo $file->current();
Upvotes: 2