lip9000 Posted November 27, 2008 Share Posted November 27, 2008 Basically I want to check a bunch of text for certain swear words, and if it contains at least 1 word, then throw out an error. All I can find on the net is replacing text with other text, I dont want to replace it, I just want to see if it's there. So maybe have an array full of swear words, then after the form is submitted check if that text contains any of those words, if yes, then do something. Thanks heaps in advance! Quote Link to comment Share on other sites More sharing options...
xdracox Posted November 27, 2008 Share Posted November 27, 2008 Use the strpos() function. $string = 'Find the word swear.'; if ( strpos($string, 'swear') !== false ) { echo 'I found swear!'; } Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 27, 2008 Share Posted November 27, 2008 You may want to reconsider and just replace it with !@$# characters (or whatever) as some words can contain other words (searches for "word" will find a match in the above code for instance). If you want the search to be case insensitive, use stripos instead of strpos in the above code. Quote Link to comment Share on other sites More sharing options...
lip9000 Posted November 27, 2008 Author Share Posted November 27, 2008 So if I wanted to use multiple different words would the code go like this? $string = 'Find the word swear.'; if ( stripos($string, 'swear,this,that,another,badword,andthis') !== false ) { echo 'I found swear!'; } ??????? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 27, 2008 Share Posted November 27, 2008 No, you could do something like <?php function wordsExist(&$string, $words) { foreach($words as &$word) { if(stripos($string, $word) !== false) { return true; } } return false; } $string = 'Find the word swear.'; if (wordsExist($string, array('swear','this','that','another','badword','andthis'))) { echo 'I found one of the words!'; } ?> Quote Link to comment Share on other sites More sharing options...
lip9000 Posted November 27, 2008 Author Share Posted November 27, 2008 Outstanding! Thanks heaps! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 27, 2008 Share Posted November 27, 2008 You can do this if you want: // Array of Bad words $words = array('fuck','shit','damn','ass'); // Array of extention to words $exten = array('','ed','ing','s'); // Input string $str = 'this is a fucking sentence'; // Create an array from input $string = explode(' ',strtolower($str)); // Create a new array for all combinations of swear words $wordList = array(); // Add all combinations to the new array foreach($words as $word){ foreach($exten as $ext){ $wordList[] = $word.$ext; } } // Loop through each input word, and check if it is a bad word or not // FALSE = Good Words // TRUE = Bad Words $badWord = FALSE; foreach($string as $s){ if(in_array($s, $wordList)){ $badWord = TRUE; } } // Do something if output is good or bad in this case display a message if($badWord) echo 'This input contains inapropriate content'; else echo 'This is valid input!'; 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.