PHP:循环substr_count

时间:2014-09-07 18:57:32

标签: php foreach

我试图查看文档中某些单词出现的次数

    //txt file of words and symbols i want to search for in a document (words are 
    //separated with a return no spaces before or after the words)

    $searchWords = file("include/words.txt");

    $content = $contentOrig = "lots of words...";

    foreach($searchWords as $word)
    {
        echo $word . ": ";
        echo substr_count($content, $word) . "<br />";
        $content = $contentOrig;
    }

没什么好看的......只是想让它在网页上输出。我得到列表中所有单词的0,除非列表中的最后一个单词出现,它给出了我对该单词的准确计数。谢谢你的期待。

1 个答案:

答案 0 :(得分:0)

您不会将单词列表分成数组。所以,让我们说每个单词都有换行符

word1
word2
word3

您需要使用explode

$wordList = explode("\n", $searchWords);
foreach($wordList as $word) {

}

请注意,它也可以是空格或\r\n。我不知道你的文本文件,我不能说。