计算单词在PHP文本中出现的频率

编程入门 行业动态 更新时间:2024-10-28 11:20:36
本文介绍了计算单词在PHP文本中出现的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在php中,我需要加载一个文件并获取所有单词,然后回显该单词以及每个单词在文本中显示的次数, (我还需要它们以降序显示最常用的单词在顶部)★✩

In php I need to Load a file and get all of the words and echo the word and the number of times each word shows up in the text, (I also need them to show up in descending order most used words on top) ★✩

推荐答案

以下是示例:

$text = "A very nice únÌcÕdë text. Something nice to think about if you're into Unicode."; // $words = str_word_count($text, 1); // use this function if you only want ASCII $words = utf8_str_word_count($text, 1); // use this function if you care about i18n $frequency = array_count_values($words); arsort($frequency); echo '<pre>'; print_r($frequency); echo '</pre>';

输出:

Array ( [nice] => 2 [if] => 1 [about] => 1 [you're] => 1 [into] => 1 [Unicode] => 1 [think] => 1 [to] => 1 [very] => 1 [únÌcÕdë] => 1 [text] => 1 [Something] => 1 [A] => 1 )

和utf8_str_word_count()函数(如果需要):

And the utf8_str_word_count() function, if you need it:

function utf8_str_word_count($string, $format = 0, $charlist = null) { $result = array(); if (preg_match_all('~[\p{L}\p{Mn}\p{Pd}\'\x{2019}' . preg_quote($charlist, '~') . ']+~u', $string, $result) > 0) { if (array_key_exists(0, $result) === true) { $result = $result[0]; } } if ($format == 0) { $result = count($result); } return $result; }

更多推荐

计算单词在PHP文本中出现的频率

本文发布于:2023-11-12 02:51:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   频率   PHP   文本中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!