Bad Word Filter, how do I replace words by their length?

后端 未结 2 1378
情歌与酒
情歌与酒 2021-01-29 06:18

I have this PHP Code, it generates random characters where there is a bad word:

$wordreplace = array (
  \"!\",
  \"#\",
  \"@\",
  \"&\",
  \"^\",
  \"$\",
         


        
相关标签:
2条回答
  • 2021-01-29 06:47

    for replace bad words in string you can use preg_replace_callbackto perform a regular expression search and replace using a callback.

    below example replace all bad words with *

    output :

    PHP is a ***ular general-purpose scripting language that is especially suited to web *****opment.

    <?php
    header('Content-Type: text/plain');
    
    // array of bad words to filter 
    $badwords = array('pop','devel');
    
    // strin to clean from bad words  
    $text = 'PHP is a popular general-purpose scripting language that is especially suited to web development.';
    // with /\w+/ regex we send each word to  replace_badwords function 
    $text = preg_replace_callback('/\w+/', 'replace_badwords', $text);
    
    echo $text;
    
    // replace_badwords check if string has bad word then its replace with * char or not return otrginal word
    function replace_badwords($ocr) {
      global $badwords;
      $newstr=false;
      foreach ($badwords as $key => $value) {
    
            $start = strpos($ocr[0],$value);
            if($start!== false){
                $length = strlen($value);
                $newstr = substr_replace($ocr[0],str_repeat("*",$length),$start,$length);
            }
    
            }
    
        return $newstr ? $newstr :  $ocr[0];
    
    }
    
    ?>
    
    0 讨论(0)
  • 2021-01-29 06:55

    You might be overthinking this. The whole first part of your code is meant to find a randomized replacement for the bad word. Only those last two lines matter, and you can replace them with:

    $content = preg_replace_callback(
        $badwords,
        function ($matches) {
            return str_repeat('*', strlen($matches[0]));
        },
        $content
    );
    

    To make this work, though, you need to wrap your badwords in a 'capture group' like so:

    $capture_badwords = [];
    foreach ($word in $badwords) {
        $capture_badwords[] = "/(".$word.")/";
    }
    

    This should produce an array like this:

    ["/(abadword)/", "/(anotherbadword)/", "/(afourletterword)/", ... ]
    

    preg_replace_callback lets you define a function with which you can manipulate the matched group.

    Putting this together in an example:

    $badwords = [ "/(dog)/", "/(cat)/", "/(here)/" ];
    //"#\((\d+)\)#"
    $phrase = "It is just dogs chasing cats in here.";
    
    echo preg_replace_callback(
            $badwords,
            function ($matches) {
                return str_repeat('*', strlen($matches[0]));
            },
            $phrase
        );
    

    Yields:

    It is just ***s chasing ***s in ****.

    0 讨论(0)
提交回复
热议问题