You are here: counting ocuring words in a string « PHP « IT news, forums, messages
counting ocuring words in a string

Posted by Merlin on 05/17/05 13:00

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация