I've been trying to come up with a way to accurately count a word string which includes punctuation marks. I've got close but the white-space is causing a problem. I have used a couple of functions such as str_replace and explode and I can now get an accurate count for texts with most punctuation and 'normal' white space. BUT.. If I put in three extra spaces between words the count adds 1 to the total.
$words2 = str_replace("-", "", $string); // strips a hypen
$words3 = str_replace(' ', ' ', $words2); // strips double spaces
$words = explode(' ', $words3);
This is obviously NOT a script - rather a string of text being passed through three functions to cleanse it. But as you can see it will strip two white spaces when they occur but no more. So three, four, five etc. will be counted as extra words.
What else could I try to give me an accurate count?
I am very raw at php. :'(
thanks
Richard