Linus Walden
Linus Walden

Reputation: 1

Php get String of .txt file by line

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

Answers (2)

Professor Abronsius
Professor Abronsius

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

kamal pal
kamal pal

Reputation: 4207

You may use SplFileObject, see example below:

$file = new SplFileObject('example.txt');
$file->seek(1);
echo $file->current();

Upvotes: 2

Related Questions