counting ocuring words in a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux