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 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 16
.
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.