I would advise to use Regular expressions instead:
http://webcheatsheet.com/php/regular_expressions.php[
^].
Find all matches each representing a word, and rewrite all the words in a frequency dictionary, so each word would be represented only once, and the value represents the frequency. The keys should be the strings, each representing a word. In PHP, this is easily done with array, because a PHP array is actually an
associative container. You should index array with your words and elements should represent frequencies:
http://php.net/manual/en/language.types.array.php[
^].
Now, you scan all Regex matches, get all the words one by one and add to array. If the key does not yet exist, you just add new word with the frequency value of 1, if it does exist, you access existing array element and increment the frequency. At the end, you will have the array with all the words and their frequencies. I hope the search for top 3 you can design by yourself.
—SA