如何在php中检查一个单词在txt文件中出现的次数?
如何检查一个单词在txt/log文件中出现的次数 例如: 110.90.252.35--[2007-05-01 10:10:55]“获取文章/学习PHP基础知识HTTP/1.0”200 11178“MSIE 7.0” 23.18.147.37--[2007-05-01 10:54:33]“了解/contact.php HTTP/1.0”200 4326“Mozilla/4.0” 250.69.170.251--[2007-05-01 11:38:11]“获取文章/not/a/page HTTP/1.0”4040“Mozilla/4.0” 从日志文件中取出三条语句,我试图查看“articles”一词在这个文件中出现了多少次。我尝试使用一个数组,然后计算它出现的次数,但到目前为止还没有成功。那么还有别的办法吗 我的代码:如何在php中检查一个单词在txt文件中出现的次数?,php,logfile,Php,Logfile,如何检查一个单词在txt/log文件中出现的次数 例如: 110.90.252.35--[2007-05-01 10:10:55]“获取文章/学习PHP基础知识HTTP/1.0”200 11178“MSIE 7.0” 23.18.147.37--[2007-05-01 10:54:33]“了解/contact.php HTTP/1.0”200 4326“Mozilla/4.0” 250.69.170.251--[2007-05-01 11:38:11]“获取文章/not/a/page HTTP/
enter code here
$mayFile = "C:\Users\Elsa\Desktop\TMA\may.log";
$myfile = fopen("may.log", "r");
$lines = count(file("may.log"));
echo "There are $lines lines";
while(!feof($myfile)) {
$getFile = fgets($myfile);
$parts = explode(" ",$getFile);
$frequency = array_count_values($parts);
print_r($parts);
$items = array_count_values($parts);
}
fclose($myfile);
fclose($myfile1);
?>
应该有一个php函数:
这样做容易得多:
$filename = "C:\Users\Elsa\Desktop\TMA\may.log";
$searchFor = "articles";
$fileContent = file_get_contents($filename);
$count = substr_count($fileContent, $searchFor);
echo "'$filename' contains '$searchFor' $count times";