ArizonaJohn Posted June 11, 2009 Share Posted June 11, 2009 Hello, The function below does a great job of blocking porn terms. However, it also blocks words that contain porn terms, such as "Essex." How could I allow "Essex" but block "sex", and allow "chicken breast" but block "breast", etc.? Thanks in advance, John <?php function check_porn_terms($input){ $porn_terms = array("porn", "sex", "tits", "cock", "penis", "vagina", "pussy", "itakeithard", "hard_cock", "really_hard_cock", "suckmydickbitch", "fuck"); //add terms here foreach($porn_terms as $term){ if(substr_count($input, $term) > 0) return false; } return true; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161783-allowing-essex-but-blocking-sex/ Share on other sites More sharing options...
proud Posted June 11, 2009 Share Posted June 11, 2009 <?php $settings['replace'] = array ("bastard" => "b******", "shit" => "s***", "sex" => "s**"); function filter_bad_words($text) { global $settings; foreach ($settings['replace'] as $k => $v) { $text = preg_replace("/\b$k\b/i",$v,$text); } return $text; } $answer="Essex sex"; $answer = filter_bad_words($answer); echo $answer; ?> Quote Link to comment https://forums.phpfreaks.com/topic/161783-allowing-essex-but-blocking-sex/#findComment-853656 Share on other sites More sharing options...
Daniel0 Posted June 11, 2009 Share Posted June 11, 2009 proud, yours is filtering, not blocking. This should do: function check_porn_terms($input) { $porn_terms = array("porn", "sex", "tits", "cock", "penis", "vagina", "pussy", "itakeithard", "hard_cock", "really_hard_cock", "suckmydickbitch", "fuck"); //add terms here return !preg_match('#\b(' . join('|', array_map('preg_quote', $porn_terms)) . ')\b#i', $input); } var_dump(check_porn_terms('essex')); // true var_dump(check_porn_terms('sex')); // false Quote Link to comment https://forums.phpfreaks.com/topic/161783-allowing-essex-but-blocking-sex/#findComment-853662 Share on other sites More sharing options...
gizmola Posted June 11, 2009 Share Posted June 11, 2009 I agree with proud that regex is the way to go, What he has provided is powered by the "\b" metacharacter, which in regex matches a "word boundary". The basic regex that does the work, is actually "\b$var\b". If you haven't used the preg_ functions before, you need to wrap the string in a begin and end character, which in his example is the "/" character. The last "i" character is a modifier that basically says: match case insensitively. If you want to retain the strategy of your function, rather than doing replacements, this is probably closer to what you want to do: function check_porn_terms($input){ $porn_terms = array("porn", "sex", "tits", "cock", "penis", "vagina", "pussy", "itakeithard", "hard_cock", "really_hard_cock", "suckmydickbitch", "fuck"); //add terms here $porn_regex = array(); foreach ($porn_terms as $term) { $porn_regex[] = '\b' . $term . '\b'; } $porn_regex = '/' . implode('|', $porn_regex) . '/i'; if (preg_match($porn_regex, $input)) { // A bad word found return false; } else { // String ok return true; } } Note: didn't check that this code actually works In this case I make one big regex string and only invoke the regex library once, rather than invoking it for every single bad word in your list as was done in proud's snippet. My guess is that this will be more efficient, although if you want a search/replace feature, proud's is the way to go. Quote Link to comment https://forums.phpfreaks.com/topic/161783-allowing-essex-but-blocking-sex/#findComment-853665 Share on other sites More sharing options...
gizmola Posted June 11, 2009 Share Posted June 11, 2009 Well lookie at that, while I was typing Daniel did the same thing, but even more elegantly. Quote Link to comment https://forums.phpfreaks.com/topic/161783-allowing-essex-but-blocking-sex/#findComment-853667 Share on other sites More sharing options...
ArizonaJohn Posted June 11, 2009 Author Share Posted June 11, 2009 Hi Daniel0, Thanks for your response, your code works in allowing "essex" but blocking "sex." However, I would also like to block "breast" but allow "chicken breast", etc. Does anyone know a way to modify the code Daniel0 gave me to do this? Thanks, John Quote Link to comment https://forums.phpfreaks.com/topic/161783-allowing-essex-but-blocking-sex/#findComment-853997 Share on other sites More sharing options...
gizmola Posted June 11, 2009 Share Posted June 11, 2009 There's no good solution to the issue of phrases, nor for that matter, simple variations to get around the filter. My suggestion would be to simply focus on the extreme vulgarity. Quote Link to comment https://forums.phpfreaks.com/topic/161783-allowing-essex-but-blocking-sex/#findComment-853999 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.