The number of words in the text?

Possible duplicate:
php: sort and count word instances in a given string

I want to write a php function that takes a string as input, splits it into words and then returns an array of words sorted by the frequency of occurrence of each word.

What is the most algorithmically efficient way to accomplish this?

+5
source share
1 answer

Best of all is:

Example

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

Output

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

CW marking because the question is a duplicate of at least two other questions containing the same answer

+22
source

All Articles