Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Checking for keyword

    Hey, this is simple enough:

    $keyword="ihatephp";
    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?

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    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() 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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I am trying to find a keyword in $message =X Ill look up that function now, thanks for reply

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Actually, that was a weblink to the strstr() function in the php manual.
    Basically it should look something like this:

    PHP Code:
    if ( strstr($message,$keyword) )
    {
          
    //do something

    There are other functions you could use for this too such as
    strpos() - tries to find the position of the needle in your string
    stristr() - which is basically the same as above but its case insensitive.
    substr_count() - counts how many times the needle is in the string. (doesn't include strings that overlap PHP5)
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You can use wildcards in regular expressions, but strstr()/stristr() should work for what he's trying to accomplish.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •