2017-05-14 58 views
1

我有一个可以去除亵渎词的工作函数。str_replace匹配后跟空格或特殊字符

单词列表由1700个坏词组成。

我的问题是,它审查

'BADWORDS'

但不

'BADWORDS。' ,'坏词'等。

如果我选择后

$ BADWORD [$关键] = $字以除去空间;

代替

$ BADWORD [$关键] = $字“。“;

那么我将有一个更大的问题,因为如果不好的话是CON那么它会剥夺一个字

我的问题是,我怎么能剥夺一个字,接着除特殊字符空间?

badword。 badword#badword,

function badWordFilter($data) 
{ 
    $wordlist = file_get_contents("badwordsnew.txt"); 
    $words = explode(",", $wordlist); 


    $badword = array(); 
    $replacementword = array(); 


    foreach ($words as $key => $word) 
    { 
     $badword[$key] = $word." "; 
     $replacementword[$key] = addStars($word); 
    } 


    return str_ireplace($badword,$replacementword,$data); 
} 


function addStars($word) 
{ 
    $length = strlen($word); 

    return "*" . substr($word, 1, 1) . str_repeat("*", $length - 2)." " ; 
} 
+2

你不想使用正则表达式吗? – splash58

+0

@ splash58只要它能工作,我就可以用它:) –

+0

preg_replace()..它的工作为你尝试这个.. –

回答

0

我能回答我自己的答案@maxchehab的帮助的问题,但我不能宣布他的答案,因为它有故障的一些区域。我张贴这个答案,以便其他人可以使用这段代码,当他们需要一个坏字过滤器。

function badWordFinder($data) 
{ 
    $data = " " . $data . " "; //adding white space at the beginning and end of $data will help stripped bad words located at the begging and/or end.   

    $badwordlist = "bad,words,here,comma separated,no space before and after the word(s),multiple word is allowed"; //file_get_contents("badwordsnew.txt"); // 
    $badwords = explode(",", $badwordlist); 

    $capturedBadwords = array(); 


    foreach ($badwords as $bad) 
    { 
     if(stripos($data, $bad)) 
     { 
      array_push($capturedBadwords, $bad); 
     }    
    } 

    return badWordFilter($data, $capturedBadwords); 
} 


function badWordFilter($data, array $capturedBadwords) 
{ 

    $specialCharacters = ["!","@","#","$","%","^","&","*","(",")","_","+",".",","," "]; 

    foreach ($specialCharacters as $endingAt) 
    { 
     foreach ($capturedBadwords as $bad) 
     { 
      $data = str_ireplace($bad.$endingAt, addStars($bad), $data); 
     }     
    } 

    return trim($data); 
} 


function addStars($bad) 
{ 
    $length = strlen($bad); 

    return "*" . substr($bad, 1, 1) . str_repeat("*", $length - 2)." "; 
} 


$str = 'i am bad words but i cant post it here because it is not allowed by the website some bad words# here with bad. ending in specia character but my code is badly strong so i can captured and striped those bad words.'; 



echo "$str<br><br>"; 

echo badWordFinder($str); 
2

假设$data是需要被审查文本,badWordFilter()将返回不好的话文本为*

function badWordFilter($data) 
{ 
    $wordlist = file_get_contents("badwordsnew.txt"); 



    $words = explode(",", $wordlist); 

    $specialCharacters = ["!","@","#","$","%","^","&","*","(",")","_","+",".",",",""]; 

    $dataList = explode(" ", $data); 

    $output = ""; 

    foreach ($dataList as $check) 
    { 
     $temp = $check; 
     $doesContain = contains($check, $words); 
     if($doesContain != false){ 
      foreach($specialCharacters as $character){ 
       if($check == $doesContain . $character || $check == $character . $doesContain){ 
        $temp = addStars($doesContain); 
       } 
      } 
     } 

     $output .= $temp . " "; 
    } 


    return $output; 
} 

function contains($str, array $arr) 
{ 
    foreach($arr as $a) { 
     if (stripos($str,$a) !== false) return $a; 
    } 
    return false; 
} 


function addStars($word) 
{ 
    $length = strlen($word); 

    return "*" . substr($word, 1, 1) . str_repeat("*", $length - 2)." " ; 
} 

Sandbox

+0

就像一个魔术般的最大值。非常感谢 –

+0

oops。我错了。不断变成~~ o ~~ stant –

+0

你能提供更多的信息吗?我将自己的“badwordsnew.txt”替换为提供的沙盒链接中的一个字符串。确保您使用的是我的答案中发布的代码。 – 2017-05-14 19:41:50