If I have an array of bad words: $badwords = array( "bad" , "killgood" , "slaughter"); and a variable: $check Where he variable $check can be a string like: "The bad man killed everyone good" What is the most efficient way to loop through the $badwords array to see if a word in $check exists in $badwords and if it does - then do (or not do) something? Thanks!
This is how I would do it: foreach($badwords as $suspect){ if(strpos($check,$suspect) !== false){ //the bad word is in there, do something } } PHP: There are a ton of ways to do it, but strpos is the quickest function if you only need to find if needle is in haystack.
Well as far as I've heard this method is more efficient ...And I wish I knew why it isn't formatting my code correctly..
Ok Guys thanks.....now that I got that working I have another problem, matching case.......How would I match words no matter the case as in uppercase or lower case or a mixture of both? ....and do it as efficiently and as fast as possible?