View Full Version : Checking for keyword

08-14-2007, 06:51 PM
Hey, this is simple enough:

if($message == "*$keyword*") {

I'm trying to find a keyword in a message and if it finds it displays an event. But this doesn't seem to be working, any ideas?

08-14-2007, 07:05 PM
Are you trying to find a keyword IN $message? or trying to see if $message and $keyword or the same? You are doing the latter. If you would like to do the former you can use something like the strstr() (http://www.php.net/strstr) function. Also, are you trying to use * as a wildcard? If so, as far as my knowledge extends I do not believe thats going to happen.

08-14-2007, 07:07 PM
I am trying to find a keyword in $message =X Ill look up that function now, thanks for reply

08-14-2007, 07:25 PM
Actually, that was a weblink to the strstr() function in the php manual. :)
Basically it should look something like this:

if ( strstr($message,$keyword) )
//do something

There are other functions you could use for this too such as
strpos() (http://www.php.net/strpos) - tries to find the position of the needle in your string
stristr() (http://www.php.net/stristr) - which is basically the same as above but its case insensitive.
substr_count() (http://www.php.net/manual/en/function.substr-count.php) - counts how many times the needle is in the string. (doesn't include strings that overlap PHP5)

08-14-2007, 11:02 PM
You can use wildcards in regular expressions, but strstr()/stristr() should work for what he's trying to accomplish.