从用户输入字符串中删除所有坏词(脏话)?

时间:2013-01-02 06:15:54

标签: php string

我试图从字符串中取出发誓的话,并认为我已经完成了,直到此网站上有人向我展示:http://codepad.org/LMuhFH4g

那么,有什么办法可以让我听清楚所有发誓的话语。

$a = array( 'duck', 'chit', 'dsshole' ); 

$str = 'duchitck-you-dssduckhole'; 

$newString = str_ireplace($a,'',$str); 
$newString = str_ireplace('-','',$newString); 
$newString = trim($newString); 
echo $newString;  

5 个答案:

答案 0 :(得分:10)

简单的解决方案是传递第四个可选的$ count参数。

do { 
    $str = str_ireplace(..., ..., ..., $count);
} while ($count); 

要真正删除诅咒词......祝你好运。有太多的变化可以完全过滤自然语言(单词,werd,w0rd,w3rd等等 - 如果有人想要给某个人打个名字,他们会找到一种方法;网站倾向于使用审核原因)


这种方法基本上是不可用的,因为它没有实际单词的概念,只有字符串(assassinate - > inate)。您可以使用正则表达式(方便的\b字边界),但在一天结束时, it's all pointless anyway

答案 1 :(得分:1)

一种审查txt文件中脏话的功能

PHP:

function censor($string)
{
    if ($string)
    {
        $badwords = file_get_contents("badwords.txt");
        $badwords = explode(",", $badwords);
        $replacewith = array();
        $index = 0;
        foreach ($badwords as $value) {
            $lengthOfStars = strlen($badwords[$index]) - 2;
            $replacewith[$index] = substr($badwords[$index], 0, 1).str_repeat("*", $lengthOfStars).substr($badwords[$index], -1);
            $index++;
        }
        $newstring = str_ireplace($badwords, $replacewith, $string);
        return $newstring;
    }
}

echo censor("Some swear words to censor");

badwords.txt:

誓言,话语

结果:

Some s***r w***s to censor

答案 2 :(得分:0)

使用this answer中的“包含”功能,您可以

$strFromSearchBox = 'duchitck you dssduckhole';
$theseWords = array('duck', 'chit', 'dsshole');

$newString = $strFromSearchBox;
while(contains($newString, $theseWords)) {
    $newString = str_replace($theseWords,'',$newString);
}

echo $newString;

答案 3 :(得分:0)

// array of all the banned strings
$swears = array(
        "a*s",
        "t*****s"
        // add all your swear words to this array
    );

$dirtyStr = "a*s and t*****s";

// remove all the banned strings
$cleanStr = str_replace($swears, '', $dirtyStr);

echo $dirtyStr;
> a*s and t*****s

echo $cleanStr;
> and

答案 4 :(得分:0)

test <- structure(list(col1 = c(2, 5, 9, 13), col2 = c(0.02, 0.12, 0.91, 
1.13)), .Names = c("col1", "col2"), row.names = c(NA, -4L), class = "data.frame")