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

后端 未结 2 1385
情歌与酒
情歌与酒 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.

     $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];
    
    }
    
    ?>
    

提交回复
热议问题