|
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
[Back to original message]
|