To count words in a php string usually we can use str_word_count but I think not always a good solution
$var =\"Hello world!\";
echo str_
I know the question is old, Still i m sharing the fix i have adopt for this.
$str ="Hello world !";
// you can include allowed special characters as third param.
print_r(str_word_count($str, 1, '!'));
code output is
Array ( [0] => Hello [1] => world [2] => ! )
if you want to include more words u can specify as third param.
print_r(str_word_count($str, 1, '0..9.~!@#$%^&*()-_=+{}[]\|;:?/<>.,'));
from 0..9. will include all numbes, and other special characters are inserted individually.