Tell me how to count the number of unique words in the text? This function counts all the words in the text:

count($rows); 

Closed due to the fact that the question is too common for participants Visman , user194374, Denis Bubnov , Denis , tutankhamun 8 Dec '16 at 12:23 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    depending on what you have in $rows . if key => value (word) - then through php.net/manual/ru/function.array-unique.php ........ if there are whole lines, then it is different. need more information - Alexey Shimansky
  • There the text received from the form (input). There is no key - Tanya
  • one
    if the text is from input, then count cannot count the number of words. Need more information that you have, that where it is transmitted. ....... if with the Input the words separated by a space are separated, then at the beginning apply explode , trim and then array_unique , count - Alexey Shimansky
  • one
    $ rows = explode ("", $ a); // counted the words (unique) echo count (array_unique ($ rows)). '<br>'; Still considers the last space as a word - Tanya
  • one
    how to count - 1. split into words, 2. sort, 3. remove duplicates, 4. count the remaining words. - aleksandr barakin

1 answer 1

If $rows is a string:

 $rows = "Hello frind, you're looking looking looking good today!"; $couns_words = str_word_count($rows, 1); $unique_array = array_count_values($couns_words); print_r($unique_array); $count_unique = count($unique_array); print_r($count_unique); //или $count_unique = count(array_unique($couns_words)); print_r($count_unique); 

Or something like:

 $couns_words = preg_split('/\s+/',$rows); $count_unique = count(array_unique($couns_words)); print_r($count_unique);