PHP Replacing swear words with phrases

前端 未结 4 1100
旧时难觅i
旧时难觅i 2021-01-17 02:18

So I get how to replace certain words with other ones. What I\'m trying to figure out is how to take a word and replace it with a phrase and eliminate all other input.

4条回答
  •  不知归路
    2021-01-17 02:59

    I've been looking at the same issue recently, here's a script I was working on to filter certain words. Still a work in progress but it has the ability to output the user message or a custom message. Hope it helps or points you in the right direction.

    define("MIN_SAFE_WORD_LIMIT", 3);
    $safe = true;
    $whiteList = array();
    $blackList = array();
    
    $text = 'Test words fRom a piece of text.';
    
    $blCount = count($blackList);
    
    for($i=0; $i<$blCount; $i++) {
        if((strlen($blackList[$i]) >= MIN_SAFE_WORD_LIMIT) && strstr(strtolower($text), strtolower($blackList[$i])) && !strstr(strtolower($text), strtolower($whiteList[$i]))) {
            $safe = false;
        }
    }
    
    if(!$safe) {
        // Unsafe, flag for action
        echo 'Unsafe';
    } else {
        echo $text;
    }
    

提交回复
热议问题