0

我有一个名为 tracker.txt 的文件,其中包含 3 行。txt 文件是这样的: http: //tracker.cpcheats.co/rookie/tracker.txt。我正在使用 file_get_contents 并爆炸以返回数组的每一行,如下所示:

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

然后我有一个 if 语句,条件是如果 tracker.txt ($status) 的第一行是“找到”,那么它将在图像上写出第二行和第三行。它不起作用。

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);
}

奇怪的是,如果我只打印 $status、$server 和 $room 而不使用 if 语句,它可以正常工作并显示正确的行。为什么它不适用于条件?因为,我确定http://tracker.cpcheats.co/rookie/tracker.txt的第一行是“找到”的。

4

2 回答 2

2

您应该尝试使用trim删除空白区域

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

修剪前

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

// 修剪后

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

你能看到长度是$status不同length=6length=5吗?

你也可以这样做

if (trim($status) == 'found') {
    // .... 
}
于 2012-10-01T01:15:49.960 回答
0

看起来你有一个额外的 \r。这有效:

if ($status == "found\r"){
   ...
}
于 2012-10-01T01:20:30.433 回答