frosty frosty - 1 year ago 64
PHP Question

Counting contractions as 2 words instead of 1 in str_word_count()

I'm trying to get the word counts of a string, but I want to count contractions as 2 words instead of 1. Is there a way to do this with str_word_count()?

$string = "i'm not";
$count = str_word_count($string);

echo $count;



Want Result:


Answer Source

As you can see in this article, there are many contractions in the English language (some I've never seen before - some are not used anymore). So the following suggestion is not exhaustive, by far.

You can decide to ignore most, and concentrate on n't, 'd, 're, 's, 'll - feel free to add more. Then, count the words in the string (using str_word_count) and search for the above sub-strings, adding 1 for each found.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download