使用if语句,PHP file_get_contents和explode无法正常工作
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("
",$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'.
我有一个名为tracker.txt的文件,其中包含3行。 txt文件是这样的: http://tracker.cpcheats.co/rookie/tracker.txt 一>。 我正在使用file_get_contents并使用explode来返回数组的每一行,如下所示: p>
然后我有一个if语句,条件是如果tracker.txt($ status)的第一行是'found',那么它会将第二行和第三行写出到图像上 。 它不起作用。 p>
奇怪的是,如果我只打印$ status,$ server和$ room而没有if语句,它可以正常工作并发出正确的行。 为什么不适应这种情况呢? 因为,我确定 http://tracker.cpcheats.co/rookie/tracker的第一行 .txt 已“找到”。 p>
div>
$ read = file_get_contents('tracker.txt');
$ filed = explode( “
”,$ read);
$ status = $ filed [0];
$ server = $ filed [1];
$ room = $ filed [2];
code> pre>
if($ status =='found'){
// //写出服务器机房和语言(带阴影)
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);
}
code > pre>
You should try using trim
to remove empty white space
$string = file_get_contents("http://tracker.cpcheats.co/rookie/tracker.txt");
$string = explode("
", $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') {
// ....
}
Looks like you have an extra . This works:
if ($status == "found"){
...
}