note 21296 deleted from function.substr-count by sander

From: Date: Tue, 07 May 2002 10:55:00 +0000
Subject: note 21296 deleted from function.substr-count by sander
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-30291@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

« previous php.notes (#30291) next »