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_
You may also use the below code it's working for me.
function get_num_of_words($string) {
$string = preg_replace('/\s+/', ' ', trim($string));
$words = explode(" ", $string);
return count($words);
}
$string="php string word count in simple way";
echo $count=get_num_of_words($string);
The result will be 7