介绍
本篇文章为大家展示了使用PHP怎么计算数组中单词出现的频率,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
& lt; PHP ? $ str =, file_get_contents(“/道路//file.txt");,//get string 得到文件 preg_match_all (“/\ b (\ w + [-] \ w +) | (\ w +) \ b/? str美元$ r);,//place words into array r 美元;作用;却;能够includes hyphenated 单词=$ words 中的键(到(“strtolower", $ r [0])),,//create new array 作用;with case-insensitive 计数 arsort(单词);,//order 得到high 用低 print_r ()
如果是大文件,读入内存就不合适了,可以采用如下方法:
& lt; php ? 时间=美元filename “/道路//file.txt"; $ handle =, fopen(文件名,美元“r"); if (handle 美元;===,false), { ,退出; } $ word =,““ while (false !==, ($ letter =,获取(处理)美元)),{ ,if (letter 美元;==,& # 39;,& # 39;),{ 美元才能结果[词]美元+ +; 时间=美元才能word “,“; ,} ,else { 美元才能word 。=,美元的信; ,} } 文件关闭($处理); print_r ($ results);
上述内容就是使用PHP怎么计算数组中单词出现的频率,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。