Jump to content

Allowing "Essex" but blocking "sex"


ArizonaJohn

Recommended Posts

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;
}

?>

Link to comment
Share on other sites

<?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;


?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :D

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.