-->

PHP更换发誓用短语话(PHP Replacing swear words with phrases

2019-07-22 09:39发布

所以我得到如何与其他的替代某些词。 我试图找出是如何采取一个字和一个词替换它,并消除所有其它输入。

例如:

坏词是“狗”

用户输入 - >“你闻起来像一只狗。”

相反,它与“彩虹”或更换的东西“狗”,我希望它呼应是这样的:“你是一个便盆口”。

下面是我有下面的代码:

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

有了这个代码,它回声:“你闻起来像你是一个pottymouth。”

我敢肯定,这是一个重新发布,我道歉。 一切我已经能够找到的是如何替换字符串,而不是整个的人只有部分文件。

Answer 1:

那么,在这种情况下,你可以检查是否有一个“坏词”,在用户输入的字符串,如果返回true,echo“你在一个不起眼的嘴。”

您可能需要使用strpos()

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

如果你有“脏话”的数组,你会希望通过他们循环来检查用户输入的任何发生。



Answer 2:

最近我一直在看同一个问题,这里有一个脚本我工作的过滤某些词。 仍然在进展中的工作,但它有能力输出的用户消息或自定义消息。 希望它可以帮助或指向你在正确的方向。

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


Answer 3:

你不想更换不好的话,但整个字符串,所以你只要匹配,如果匹配设置整串到您的替换字符串。

此外,如在评论中指出的话可以是另一种有效的,字一部分,所以如果你要考虑到这一点,你应该只匹配整个单词。

这个简单的例子使用单词边界的正则表达式匹配你的话(在本例中,这将是在一个循环,循环您的脏话阵列):

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


Answer 4:

下面有一个替代的解决方案,匹配词和与STR的* LEN替换。 这不会匹配的话就像斯肯索普,因为它使用单词边界,你还可以添加一个第三个参数,让你知道该用什么词,没有看到有人说揭示了单词的第一个字母。

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


文章来源: PHP Replacing swear words with phrases