I wrote a function which is better than str_word_count
because that PHP function counts dashes and other characters as words.
Also my function addresses the issue of double spaces, which many of the functions other people have written don't take account for.
As well this function handles HTML tags. Where if you had two tags nested together and simply used the strip_tags
function this would be counted as one word when it's two. For example: <h1>Title</h1>Text
or <h1>Title</h1><p>Text</p>
Additionally, I strip out JavaScript first other wise the code within the <script>
tags would be counted as words.
Lastly, my function handles spaces at the beginning and end of a string, multiple spaces, and line breaks, return characters, and tab characters.
###############
# Count Words #
###############
function count_words($str)
{
$str = preg_replace("/[^A-Za-z0-9 ]/","",strip_tags(str_replace('<',' <',str_replace('>','> ',str_replace(array("\n","\r","\t"),' ',preg_replace('~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is','',$str))))));
while(substr_count($str,' ')>0)
{
$str = str_replace(' ',' ',$str);
}
return substr_count(trim($str,' '),' ')+1;
}
count(s($str)->words())
helpful, as found in this standalone library. – Haar