PHP从文件计数(某些单词)

时间:2015-03-01 17:42:19

标签: php file count

我有一个奇怪的问题需要克服,它很简单,但我似乎无法找到任何解决方案或解决这个问题:我有一个.html文件,我想阅读某些“单词”并计算多少肯定有的话。

提前致谢

只是试着计算某些单词以防止我的应用程序出现故障。

1 个答案:

答案 0 :(得分:0)

你没有说明你是在搜索多个单词,还是只搜索某个单词,所以我要去多个单词。

$count = 0; //Create a variable for counting
$contents = file_get_contents("yourfile.html"); //Load your file into a string
$words = array('word1', 'word2', 'word3');  //Specify which words you want to search for

foreach($words as $word) {  //For each word in array do
$place = strpos($contents, $word); //Get the position of searched word
if (!empty($place)) { //if a word has been found, increment the count variable
    $count++;
} 
}
echo 'Number of filtered words: ' . $count; //Print out the number of words