Re: Word Count

From: Date: Thu, 23 Jan 2003 18:15:41 +0000
Subject: Re: Word Count
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-132877@lists.php.net to get a copy of this message
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" <chris@wild.net> 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 sbedberg@ucdavis.edu | | 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... | +------------------------------------------------------------------------+

« previous php.general (#132877) next »