开发者

Imported, Exploded Word List Doesn't Compare Properly

My original test implementation consisted of building an array of "ignore words" with the following code:

$ignoreList = array("test1",开发者_运维知识库 "test2", "test3");

Later on, I test for individual words in the $ignoreList:

if(in_array($word, $ignoreList)){
    } else{
    $words[$word] = $words[$word] + 1;
}

This code works perfectly - upon later echoing my word list, no words on the $ignoreList show up. I refactored to make it easier to add or remove words:

//Import ignore list
$ignore_raw = file_get_contents("includes/ignore.txt");
$ignoreList = explode("\n", $ignore_raw);

ignore.txt is a plain text file with each item on its own line, no spaces. The import and explode seems to be working, because a print_r statement on $ignoreList results in:

Array ( [0] => a [1] => and [2] => are [3] => as [4] => for [5] => in [6] => is [7] => more [8] => of [9] => than [10] => that [11] => the [12] => to [13] => with )

The comparison code, however, stops working properly, and words on the ignore list show up once again in my final results. Any ideas what's wrong?


Your ignore.txt file may have \r\n line endings, and your words actually have a trailing \r.

Try that:

$ignoreList = array_map('trim', file("includes/ignore.txt"));

BTW your code may be refactored like that:

$words = array_diff($words, $ignoreList); // removes ignored words
$words = array_count_values($words); // count words
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜