-1

这让我很沮丧,因为我对 PHP 还很陌生,而且我确信我错过了一些关于语言本质的非常基本的东西。但是为什么哦,为什么这不起作用?

$tag = $_GET['id'];
$openfile = fopen($files[$i], "r");
$tagsraw = fgets($openfile);
$tag_array = explode(",",$tagsraw);
foreach ($tag_array as $a) {
    if ($a == $tag) {
        echo $a." matches ".$tag;
    }
}

编辑:顺便说一句,文件打开工作正常;print_r()显示$tag_array了它的含义。

编辑:这是来自print_r(). 有五个文件,每个文件的第一行都有自己的标签。

Array
(
    [0] => webdesign

)
Array
(
    [0] => personal

)
Array
(
    [0] => recipes
    [1] => vegan

)
Array
(
    [0] => personal

)
Array
(
    [0] => personal

)
4

1 回答 1

4

my magic crystal ball tells me that "doesn't work" means

if ($a == $tag) {

is never true?

you probably have whitespace characters around one of them. use var_dump() to inspect the values of variables. Notice that var_dump tells you the data type, and for strings, the string length in bytes.

If there is whitespace, you can remove it using PHP's trim function:

if (trim($a) == trim($tag)) {
于 2012-05-21T14:42:15.150 回答