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!";
Array (  => Hello  => world )
$var ="The example number 2 is a bad example it will not
count numbers and punctuations !!";
Array (  => The  => example  => number  => is  => a
 => bad  => example  => it  => will  => not
 => count  => numbers  => and  => punctuations )
You can always split your string by whitespace and count the results:
$res = preg_split('/\s+/', $input); $count = count($res);
With your string
"The example number 2 is a bad example it will not count numbers and punctuations !!"
This code will produce
The advantage of using this over
explode(' ', $string) is that it will work on multi-line strings as well as tabs, not just spaces. The disadvantage is that it's slower.