counting ocuring words in a string

From: Date: Tue, 17 May 2005 10:00:24 +0000
Subject: counting ocuring words in a string
Groups: php.general 
Request: Send a blank email to php-general+get-215379@lists.php.net to get a copy of this message
Hi there, I am trying to find a way to count the number of times (if any) words are inside a string. So I played around with ereg, preg_match_all and so on, but could not put together a working code. Can anybody help me on that? This is the current code: function count_words($str, $words) { if(is_array($words)) { foreach($words as $k => $word) {
     $pattern[$k] = "/\b($word)\b/is";
} } else { $pattern = "/\b($words)\b/is"; } return ereg( $pattern, $str); } $words = 'php language'; $str = 'One language which is great is php. PHP works great!'; $num_hits = count_words($str, $word); echo $num_hits; I would like to get an case insensitive result. In this example: 3 I am a bit lost on this one. Thank you for any help. Merlin

« previous php.general (#215379) next »