0

I have a file called tracker.txt that contains 3 lines. The txt file is this: http://tracker.cpcheats.co/rookie/tracker.txt. I am using file_get_contents and explode to return each line of the array, like so:

$read = file_get_contents('tracker.txt');
$filed = explode("\n",$read);
$status = $filed[0];
$server = $filed[1];
$room = $filed[2];

I then have an if statement, where the condition is if the first line of tracker.txt ($status) is 'found', then it will write out the second and third line onto the image. It doesn't work.

if ($status == 'found') {
//write out the server room and language (with shadow)
imagettftext($im, 15, 0, 140, 80, $white, $font, $server);
imagettftext($im, 15, 0, 139, 79, $black, $font, $server);
imagettftext($im, 15, 0, 140, 105, $white, $font, $room);
imagettftext($im, 15, 0, 139, 104, $black, $font, $room);
}

The odd thing is, if I just print out $status, $server and $room without the if statement, it works fine and dispays the correct lines. Why isn't it working with the condition? Because, I'm sure the first line of http://tracker.cpcheats.co/rookie/tracker.txt is 'found'.

3
  • Does the file use DOS-style newlines (en.wikipedia.org/wiki/Newline)? i.e., are there trailing \r characters at the end of each line before the \n you're exploding on? Commented Oct 1, 2012 at 1:10
  • Just before the first block of code I gave you, I have this. $file = fopen("tracker.txt","w"); echo fwrite($file,"found\n (EN) Avalanche\nIce Berg"); fclose($file); Commented Oct 1, 2012 at 1:13
  • PHP also has the file() function, which reads the file and returns all lines in an array already. Commented Oct 1, 2012 at 1:15

2 Answers 2

2

You should try using trim to remove empty white space

$string = file_get_contents("http://tracker.cpcheats.co/rookie/tracker.txt");
$string = explode("\n", $string);
list($status, $server, $room) = array_map("trim", $string);

Before trim

array
  0 => string 'found
' (length=6)
  1 => string ' (EN) Avalanche
' (length=16)
  2 => string 'Ice Berg' (length=8)

// After Trim

array
  0 => string 'found' (length=5)
  1 => string '(EN) Avalanche' (length=14)
  2 => string 'Ice Berg' (length=8)

Can you see that the length is of $status is different length=6 and length=5 respectively

You can also just do

if (trim($status) == 'found') {
    // .... 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you have an extra \r. This works:

if ($status == "found\r"){
   ...
}

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.