Tonic-_- Posted August 7, 2009 Share Posted August 7, 2009 Mmmk, i'm working on a IRC Bot and this never occured to me on how to do it but my current bot is "disrupting" users when they have the selected word in the actual word they type.. I have my bot detect hi or hello right if someone types "this" anywhere in the sentance it makes the bot respond. Well I thought of a few ways but they would have a slim chance of working and decided i'd ask you guys. Right now it detects a whole string *Not array* and searches for the key letters in the word and I need it to parse it so it doesn't respond if its not the word by its self.. Ideas? Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 7, 2009 Share Posted August 7, 2009 you will want to look into RegEx Quote Link to comment Share on other sites More sharing options...
Tonic-_- Posted August 7, 2009 Author Share Posted August 7, 2009 Umm I guess I forgot to mention the original comparison is using eregi and I just need it to detect hi or hello as a word and not in a word. Just detect hi or hello as its OWN word and nothing else.. Looked at what you said and the description didn't seem to do anywhere close to what I wanted. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 7, 2009 Share Posted August 7, 2009 eregi is regex. if you want to detect just the word hi or hello something like $patternHi = "/^hi$/"; $patternHello = "/^hello$/"; should work (hopefully. haven't done much regex at all, but have done some with javascript, and I don't know how much different, or if there is a difference, between javascript regex and php regex) that will match the word only. now that i think of it, just having the word as the pattern would probably work too. In any case, for regex help, i'm not the one to go to. Hope that helps! Quote Link to comment Share on other sites More sharing options...
Tonic-_- Posted August 7, 2009 Author Share Posted August 7, 2009 Well I used the part you did and it kills the search period.. if (eregi("/^sex$/", $read)) Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2009 Share Posted August 7, 2009 You need to use the word boundry keyword: \b I couldn't get it to work with eregi, but I could with preg_match(). $userText = "Hi there"; //Would return true in the test below $userText = "Hit this"; //Would NOT return true in the test below $botSearch = '/\bhi\b/i'; //Added i at the end to make case insensitive if(preg_match($botSearch, $userText)!==0) { echo "Found the WORD 'hi'"; } Quote Link to comment Share on other sites More sharing options...
Tonic-_- Posted August 7, 2009 Author Share Posted August 7, 2009 You need to use the word boundry keyword: \b I couldn't get it to work with eregi, but I could with preg_match(). $userText = "Hi there"; //Would return true in the test below $userText = "Hit this"; //Would NOT return true in the test below $botSearch = '/\bhi\b/i'; //Added i at the end to make case insensitive if(preg_match($botSearch, $userText)!==0) { echo "Found the WORD 'hi'"; } Ahh! Good ol' reliable preg_match ! Alright i'll give it a go and see what I get. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted August 7, 2009 Share Posted August 7, 2009 PHPs own str_word_count() function might be an alternative to regular expressions, when used with the "format" parameter set to 2 Quote Link to comment Share on other sites More sharing options...
Tonic-_- Posted August 7, 2009 Author Share Posted August 7, 2009 W00t the preg_match came through like always, never even thought of using it. Thanks a bunch, I love this site for help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.