note 21296 added to function.substr-count

From: Date: Tue, 07 May 2002 10:26:09 +0000
Subject: note 21296 added to function.substr-count
Groups: php.notes 
Request: Send a blank email to php-notes+get-30288@lists.php.net to get a copy of this message
This function count the number of words occurrences in string function words_count($str) { # trim the text and count the occurrences of spaces $words=substr_count (trim($str)," "); # add one when spaces will be found between two ore more words # else words continue with value=0 from substr_count() if ($words>0) { $words++; } return $words; } Description int word_count (string string) word_count() returns the number of words occurs in the string. If there are no words in the string the function return 0. In reality the function return the number of spaces+1, witch is nearly similar to the number of words. It is failing when there are more than one space between words in text. Example 1: word_count() example $text="This is the text!"; $amount=word_count($text); echo "Number of words: ".$amount; Return the string: Number of words: 4 -- http://www.php.net/manual/en/function.substr-count.php http://master.php.net/manage/user-notes.php?action=edit+21296 http://master.php.net/manage/user-notes.php?action=delete+21296 http://master.php.net/manage/user-notes.php?action=reject+21296

« previous php.notes (#30288) next »