kyleabaker Posted March 30, 2007 Share Posted March 30, 2007 Hey all, sorry if this has been posted and explained before. I've searched several times, but I'm just not really even sure what to search for. I'm basically trying to define an array of "bad words" and I want to check string1 and make sure that none of the items in the array are found to occur in string1. I basically want to throw out any string1's that contain any "bad words". By bad words, I mainly mean that I'm trying to filter common words used in bot messages. So I basically don't know what to use in php to check in the string for these bad words. I'm pretty sure that I'll have to use a loop of some sort (as there are no built in functions to my knowledge that automate this type of search). I searched on the php manual site and was unsure of which function to use. I was thinking maybe use "strstr" and on the first occurrence, just throw out the message all together. But I want to make sure that this is a speedy/accurate method of doing what my goal is. I also know that I will need to make the comparisons case insensitive...and I found kind of what I'm looking for here, but am unable to really interpret it completely (still a n00b, lol).. http://fundisom.com/phpsnippets/snip/string_handling/string_in_string/ Thanks all in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/44994-solved-find-if-array-of-strings-occurs-in-another-string/ Share on other sites More sharing options...
genericnumber1 Posted March 30, 2007 Share Posted March 30, 2007 Here's an example of replacing any "bad words" with *s <?php $string = 'My favorite ice cream is chocolate'; $badWords = array('cream', 'chocolate'); // Of course you would do different words. foreach($badWords as $word) { $length = strlen($word); $replacement = str_irepeat('*', $length); $string = str_replace($word, $replacement, $string); } echo $string; // "My favorite ice ***** is *********" ?> or to just remove the words... <?php $string = 'My favorite ice cream is chocolate'; $badWords = array('cream', 'chocolate'); // Of course you would do different words. foreach($badWords as $word) { $string = str_ireplace($word, '', $string); } echo $string; // "My favorite ice is ?> To be able to throw out the message completely I guess you could do this... <?php $string = 'My favorite ice cream is chocolate'; $badWords = array('cream', 'chocolate'); // Of course you would do different words. foreach($badWords as $word) { if(stristr($string, $word) != false) { $error = true; break; } } if($error == true) { echo 'You used a bad word!'; } else { echo $string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/44994-solved-find-if-array-of-strings-occurs-in-another-string/#findComment-218416 Share on other sites More sharing options...
kyleabaker Posted March 30, 2007 Author Share Posted March 30, 2007 @genericnumber1 Thanks! I'll give it a shot with throwing them out completely. I don't really care about filtering language on my site cause I'm not worried about foul language, I just have bots posting about meds and other stuff, etc. I just wanna throw out the bot posts. No one else on my site is gonna be posting about viagra, lol. Thanks for the fast response! Quote Link to comment https://forums.phpfreaks.com/topic/44994-solved-find-if-array-of-strings-occurs-in-another-string/#findComment-218438 Share on other sites More sharing options...
kyleabaker Posted March 31, 2007 Author Share Posted March 31, 2007 @genericnumber1 Works like a charm! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/44994-solved-find-if-array-of-strings-occurs-in-another-string/#findComment-218454 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.