0

I got a simple php script that is storing the content of POST in to a text file. data.txt looks like this:

Something - Line1
Something - Line2
Something - Line3
Something - Line4

I'm trying to figure out the best way to set each line as a variable e.g.

$line1 = "Something - Line1";
$line2 = "Something - Line2";
$line3 = "Something - Line3";
$line4 = "Something - Line4;"

so I can use it for further processing. Please note that the numbers of line can change. Any ideas? :-)

Thanks!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Matt Raven
  • 31
  • 2
  • 4

2 Answers2

5

Best to use an array. Check out file():

$lines = file('/path/to/file.txt');

Then you access the lines starting from 0:

echo $lines[0];
echo $lines[1];
//etc...

Or loop through them:

foreach($lines as $line) {
    echo $line;
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

This link provides your answer: Read each line of txt file to new array element

The link makes use of the while loop and searches for the end of the file

The link provided uses the following code:

<?php
$file = fopen("members.txt", "r");
$members = array();

while (!feof($file)) {
   $members[] = fgets($file);
}

fclose($file);

var_dump($members);
?>
Community
  • 1
  • 1
ctwheels
  • 21,901
  • 9
  • 42
  • 77