2017-05-14 42 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