0

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"?

2 Answers 2

2

You may use SplFileObject, see example below:

$file = new SplFileObject('example.txt');
$file->seek(1);
echo $file->current();
Sign up to request clarification or add additional context in comments.

1 Comment

I like that - not used this before ut I like it!
0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.