Jump to content

file_get_contents line by line?


peter11

Recommended Posts

Hi all,

 

My application asks questions that it gets from a random text file however i wish to have like part 1, part 2 and part 3 of each question.

 

At the moment i am using the file_get_contents function and opening the file and echo'ing part 1 of the question. I could easily put part 2 in a different text file however it would also pick that at random and i want part 2 to match up with part one. Therefore what code should i use if i want to have a .txt file with the first part of the question on line 1 the second part on line 2 and the third on line 3. I wish to echo each line into separate a variable.

 

Thanks for the help :)

Link to comment
https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/
Share on other sites

You can do that like this using something like this:

 

$file = file('file.txt');
foreach($file as $key => $line){
    ${'line' . ($key + 1)} = $line;
}

 

But I don't see any need for that. Why create extra variables when you can just access the lines like $file[0], $file[1], etc..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.