Since I read that str_word_count is flawed, I searched for an alternate solution and came across the following, which works quite good in general except of one problem.
function count_words($text) {
//it removes html tags
$text = preg_replace('/<[^>]*>/', '', $text);
//it removes html space code
$text = preg_replace(array('/ /'), ' ', $text);
//it removes multiple spaces with single
$text = trim(preg_replace('!\s+!', ' ', $text));
return count(explode(' ', $text));
}
The problem is that it detects a dash "-" as a word.
Example:
This is a title - Additional Info
It will count 7 words instead of 6.
Is there a possibility to exclude single characters like - from this word count?