PHP - 计算字符串出现在文件中的次数 [英] PHP - Count number of times string appears in file
本文介绍了PHP - 计算字符串出现在文件中的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
funkystudios
funkystudios
funkystudios
gilletteracer74
axehairgel
Ferby123
dirdam
TheWu13
Expert_Assassin
TheWu13
ocanosoup
我希望能够读取此文件,并打印出每个人在文件中出现的次数。 (也可按文件中的次数排列)
funkystudios:3
TheWu13:2
gilletteracer74:1
axehairgel:1
(依此类推)
已经尝试了各种方式,但这一切都归结为一个问题,当我试图正确地命令他们...我猜是有一个非常简单的方法来做到这一点。 (我是新来的PHP ...)
编辑:
我已经到了这个地步:
$ b $
foreach(array_count_values(file('leaderboard.txt'))as $ person => $ count)
echo{$ person}:{$ count}< br /> \r\\\
;
它并不按$ count排序,而只是在文件中首先出现。 / p>
解决方案
$ counts = array_count_values(file('leaderboard.txt'));
arsort($ count);
$ b foreach($ count as $ person => $ count)
echo{$ person}:{$ count}< br /> \r\\\
;
I've got a file (leaderboard.txt) that looks like this:
funkystudios
funkystudios
funkystudios
gilletteracer74
axehairgel
Ferby123
dirdam
TheWu13
Expert_Assassin
TheWu13
ocanosoup
I want to be able to read this file, and print out the number of times each person appears in the file. (Also place in order of # of times in file)
funkystudios: 3
TheWu13: 2
gilletteracer74: 1
axehairgel: 1
(and so on)
I've tried various ways but It all came down to an issue when I would try to order them correctly... I'm guessing there is a pretty easy way to do this. (I'm new to PHP...)
EDIT: I have gotten to this point:
foreach(array_count_values(file('leaderboard.txt')) as $person => $count)
echo "{$person} : {$count}<br />\r\n";
It doesn't order by the $count, but simply who comes up first in the file.
解决方案
$counted = array_count_values(file('leaderboard.txt'));
arsort($counted);
foreach($counted as $person => $count)
echo "{$person} : {$count}<br />\r\n";
这篇关于PHP - 计算字符串出现在文件中的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文