...

View Full Version : How many matches of x word in message?



Boshi
08-11-2007, 12:07 AM
Hello, I have tried learning preg_match & preg_match_all, but its no use, can anyone make me a small piece of code that would count how many times x word was found in x message? Please, or at least explain me how to do one.

mcjwb
08-11-2007, 12:51 AM
How about this:

$needle="php";
$pattern="/\b".$needle."\b/i";
$haystack="does php rock or does phprock";//deliberate mising space.
print "found:".preg_match_all($pattern, $haystack, $matches)."\n"; //prints found:1 since it doesn't count phprock.

Or you could always use substr_count (http://uk2.php.net/manual/en/function.substr-count.php);)

Boshi
08-11-2007, 12:58 AM
I had no idea that function was there.... Thanks a lot anyways!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum