10

可能重复:
php:对给定字符串中单词的实例进行排序和计数

我正在寻找一个 php 函数,它将字符串作为输入,将其拆分为单词,然后返回按每个单词的出现频率排序的单词数组。

实现这一点的算法最有效的方法是什么?

4

1 回答 1

28

你最好的选择是这些:

例子

$words = 'A string with certain words occuring more often than other words.';
print_r( array_count_values(str_word_count($words, 1)) );

输出

Array
(
    [A] => 1
    [string] => 1
    [with] => 1
    [certain] => 1
    [words] => 2
    [occuring] => 1
    [more] => 1
    [often] => 1
    [than] => 1
    [other] => 1
)

标记 CW,因为问题与至少两个包含相同答案的其他问题重复

于 2011-01-12T15:22:56.837 回答