-2

I have the following code :

$stopwords = file("/path/to/my-file.txt", FILE_IGNORE_NEW_LINES);
echo($stopwords[0]." - ");

$words = explode(" ", "alors on danse");
echo($words[0]." - ");

if (in_array($words[0], $stopwords)) {
    echo("yay");
} else {
    echo("nay");
}

And I always get alors - alors - nay as a result, when I'm expecting alors - alors - yay

I've seen a few topics on here regarding similar problems and the solutions were almost always to use the trim() function on the elements of the list array. Which I tried, but it didn't change anything.

Could you please help me realize what I'm doing wrong ?

4

2 回答 2

1

似乎它仅不适用于文本文件第一行的单词

这给了我一个想法:你的 my-file.txt 是 utf8 吗?

它可以保存为带有 BOM 的 utf8 - 一个字节顺序标记,它由在被视为 utf8 时不显示为文本的字节组成,但如果它们之间没有空格字符,则将被解释为第一个单词的一部分第一个真正的词。(维基条目)(因此,不建议将 BOM 与 utf8 一起使用,但一些编辑器仍将其作为默认值)。

如果是这种情况,请尝试将文件另存为不带 BOM 的 utf8(例如使用记事本++)

结论:编码是ab*tch,有时不仅需要查看显示的文本,还需要使用十六进制编辑器或类似工具来检查字符串是否不仅看起来相似,而且真的是相同的字节序列

于 2013-07-29T12:21:29.083 回答
0

我想你想检查是否$words$list

if(in_array($words[0], $list)) {
...
}

或定义$stopwords

$stopwords = $list;
于 2013-07-26T13:41:47.590 回答