Count the occurrence of words in a string


If you want to count the occurrence of words in a string, the following routine may help you. For example if you supply the string “hello hello Hello I am am Hasin” it will out put the followng

hello = 3
I = 1
am = 2
hasin = 1

Here is the code

<?php
$str = “hello hello Hello I am am hasin”;
$word_count = (array_count_values(str_word_count(strtolower($str),1)));
ksort($word_count);
foreach ($word_count as $key=>$val)
echo $key .” = “. $val.”<br/>”;
?>