Count how often a word appears in text in PHP

In php, I need to upload a file and get all the words and repeat the word and the number of times each word appears in the text, (I also need them to appear in descending order of the most used words from above) ★ ✩

+7
php
source share
4 answers

Here is an example:

$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>'; 

Exit:

 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 ) 

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; } 
+10
source share
 $ words = str_word_count ($ text, 1);
 $ word_frequencies = array_count_values ​​($ words);
 arsort ($ word_frequencies);
 print_r ($ word_frequencies);
+3
source share

This function uses a regular expression to search for words (you can change it depending on what you define as a word)

 function count_words($text) { $output = $words = array(); preg_match_all("/[A-Za-z'-]+/", $text, $words); // Find words in the text foreach ($words[0] as $word) { if (!array_key_exists($word, $output)) $output[$word] = 0; $output[$word]++; // Every time we find this word, we add 1 to the count } return $output; } 

This is an iteration for each word, the construction of an associative array (with the word as a key), where the value refers to the occurrences of each word. (for example, $ output ['hello'] = 3 => hello 3 times in the text).

You might want to change the function to deal with case insensitivity (i.e., "hello" and "Hello" are not the same word according to this function).

+2
source share
 echo count(explode('your_word', $your_text)); 
0
source share

All Articles