To be somewhat more rigorous, you could do:

$numOfWords = count(preg_split('/\s+/', trim($string)));

(also untested); this ignores leading/trailing whitespace, and deals with multiple spaces/tabs between words. There is an example in the docs (below) called 'Get the parts of a search string' that shows how to add other characters that you might consider valid word delimiters - eg, a comma.

See
http://php.he.net/manual/en/function.preg-split.php

-steve



At 10:41 AM -0600 1/23/03, "Chris Boget" <[EMAIL PROTECTED]> wrote:
 > Is there a way to count the number of words in a string?

Untested, but this should work.

$string = "Is there a way to count the number of words in a string?"

$numOfWords = count( explode( " ", $string ));

Chris

--
+------------------------------------------------------------------------+
| Steve Edberg                                      [EMAIL PROTECTED] |
| University of California, Davis                          (530)754-9127 |
| Programming/Database/SysAdmin               http://pgfsun.ucdavis.edu/ |
+------------------------------------------------------------------------+
| SETI@Home: 1001 Work units on 23 oct 2002                              |
| 3.152 years CPU time, 3.142 years SETI user... and STILL no aliens...  |
+------------------------------------------------------------------------+

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to