1

Okay so I have a text file and inside of the text file I have these lines:

IP = 127.0.0.1
EXE = Client.exe
PORT = 8080
TITLE = Title
MAINT = False
MAINT-Message = This is the message.

what I am wanted to do is get the 'False' part on the fifth line.

I have the basic concept but I can't seem to make it work. This is what I have tried:

<?php
$file = file_get_contents('LauncherInfo.txt');
$info = explode(' = ', $file);

echo $info[5];
?>

And with this I get a result but when I echo $info[5] it gives me 'False Maint-Message' so it splits it but it only splits at the = sign. I want to be able to make it split at the where I have pressed enter to go onto the next line. Is this possible and how can I do it?

I was thinking it would work if I make it explode on line one and then do the same for the second line with a loop until it came to the end of the file? I don't know how to do this though.

Thanks.

avgcoder
  • 372
  • 1
  • 9
  • 27

2 Answers2

1

I think you're looking for the file(), which splits a file's contents into an array of the file's lines.

Try this:

$file = file('LauncherInfo.txt');
foreach ($file as $line) {
    if ($line) {
        $splitLine = explode(' = ',$line);
        $data[$splitLine[0]] = $splitLine[1];
    }
}
echo $data['MAINT'];
AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
1

Just in case you were curious, since I wasn't aware of the file() function. You could do it manually like this

<?php
$file = file_get_contents('LauncherInfo.txt');
$lines = explode("\n", $file);
$info=array();
foreach($lines as $line){
  $split=explode(' = ',$line);
  $info[]=$splitline[1];
}
echo $info[5];//prints False
?>
wonton
  • 7,568
  • 9
  • 56
  • 93