【问题标题】:PHP Replacing swear words with phrasesPHP用短语替换脏话
【发布时间】:2013-03-11 04:31:43
【问题描述】:

所以我知道如何用其他单词替换某些单词。我想弄清楚的是如何取一个词并将其替换为一个短语并消除所有其他输入。

例如:

坏词是“狗”

用户输入 -> '你闻起来像狗。'

我不想用“彩虹”或其他东西代替“狗”,而是希望它回响类似:“你是个便盆”。

这是我的代码:

<?php

$find = array('dog', 'cat', 'bird');
$replace = 'You are a potty mouth.';

if (isset ($_POST['user_input'])&&!empty($_POST['user_input'])) {
    $user_input = $_POST['user_input']; 
    $user_input_new = str_ireplace($find, $replace, $user_input);

        echo $user_input_new;
}
?>

使用此代码,它会回显:“你闻起来像你是个大嘴巴。”

我确定这是转发,我深表歉意。我能找到的所有内容都是关于如何仅替换部分字符串而不是整个字符串的文档。

【问题讨论】:

  • 我希望现在任何时候都会看到一些对此的反应。
  • 我更喜欢斯肯索普问题...
  • 很难让它在所有情况下都能正常工作,例如与“Peacock”、“Scunthorpe”等合法词的部分不匹配。

标签: php string echo words phrases


【解决方案1】:

这是另一种解决方案,匹配单词并替换为 * len of str。这不会匹配像 Scunthorpe 这样的单词,因为它使用单词边界,您还可以添加第三个参数来显示单词的第一个字母,这样您就可以在不看到它的情况下知道所说的单词。

<?php
$badwords = array('*c word', '*f word','badword','stackoverflow');

function swear_filter($str,$badwords,$reveal=null) {
    //Alternatively load from file
    //$words = join("|", array_filter(array_map('preg_quote',array_map('trim', file('badwords.txt')))));


    $words = join("|", array_filter(array_map('preg_quote',array_map('trim', $badwords))));
    if($reveal !=null && is_numeric($reveal)){
        return preg_replace("/\b($words)\b/uie", '"".substr("$1",0,'.$reveal.').str_repeat("*",strlen("$1")-'.$reveal.').""', $str);
    }else{
        return preg_replace("/\b($words)\b/uie", '"".str_repeat("*",strlen("$1")).""', $str);
    }

}


$str="There was a naughty Peacock from Scunthorpe and it said a badword, on stackoverflow";

//There was a naughty Peacock from Scunthorpe and it said a b******, on s************
echo swear_filter($str,$badwords,1);

//There was a naughty Peacock from Scunthorpe and it said a *******, on *************
echo swear_filter($str,$badwords);
?>

【讨论】:

    【解决方案2】:

    您不想替换坏词,而是替换整个字符串,因此您应该匹配,如果匹配,则将整个字符串设置为替换字符串。

    此外,正如 cmets 中所指出的,单词可以是另一个有效单词的一部分,因此如果您想考虑到这一点,您应该只匹配整个单词。

    这个简单的例子使用正则表达式中的单词边界来匹配你的单词(在这个例子中,这将是一个循环,循环你的坏单词数组):

    foreach ($find as $your_word)
    {
      $search = '/\b' . preg_quote($your_word) . '\b/i';
      if (preg_match($search, $_POST['user_input']) === 1)
      {
        // a match is found, echo or set it to a variable, whatever you need
        echo $replace;
        // break out of the loop
        break;
      }
    }
    

    【讨论】:

      【解决方案3】:

      好吧,在这种情况下,您只需检查用户输入字符串中是否存在“坏词”,如果返回 true,则回显“您是个便盆。”

      你会想要使用 strpos()

      例如

      if( strpos($_POST['user_input'],'dog')!==FALSE ) {
          echo('You are a potty mouth');
      }
      

      如果您有一个“坏词”数组,您需要遍历它们以检查用户输入中出现的任何情况。

      【讨论】:

        【解决方案4】:

        我最近一直在研究同样的问题,这是我正在编写的用于过滤某些单词的脚本。仍在进行中,但它能够输出用户消息或自定义消息。希望它对您有所帮助或指出正确的方向。

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

        【讨论】:

          猜你喜欢
          • 2021-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-27
          • 1970-01-01
          • 1970-01-01
          • 2017-01-29
          • 2011-04-27
          相关资源
          最近更新 更多