I am attempting to count the number of occurrences of every unique word on a page (think SEO 'word count' that you see on woorank etc. - but not for that purpose!)
I am really struggling on how to set this up:-
At the moment I am thinking of reading each word and then checking if it is unique against an array -> if unique add to array with occurences=>1
- then if I find the same word later just +1.
However this seems really cumbersome and slow for large blocks of text (especially as I will have to strip commas etc, convert all to lower case etc.) -> is there are a better way, has someone got a code snippet or library for this task?
For clarity
The Cat ran away with the hat. The spoon had already run away with another cat, far far away.
Would yield:
the => 3,
away => 3,
cat => 2,
with => 2,
far => 2,
spoon => 1,
hat => 1,
ran => 1,
run => 1,
had => 1,
another => 1,
already => 1
Thanks in advance - if there is no better way then that is fine!
ASIDE
I contemplated do a replace($word,"")
on all words once found and counted -> but this seems just as cumbersome.