2017-03-01 68 views
1

我有一個內容過濾器可以工作,但它不會像我想要的那樣工作。例如,如果我說「壞字你是我的壞話恨你」,整個事情輸出爲「[內容刪除]」,但我希望它說「*******你,我*******恨你「php壞詞內容過濾器

<?php 

function SecurePost($var) { 
    return trim($DBcon->real_escape_string($var)); 
} 

function filter($string) { 
    $words = array("badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere"); 
    $new = array("****", "*****", "***", "*******", "****", "****", "*****", "******", "*****", "******", "*****", "***", "*******", "***", "****", "***", "*****", "*******", "****", "****", "*****", "****", "****", "****", "******"); 
    $string1 = str_ireplace($words, $new, $string); 
    $findme = "***"; 
    $pos = strpos($string1, $findme); 
    if ($pos !== false) { 
    $string1 = "[ Content Deleted ]"; 
    return $string1; 
    } 
    else { 
    return $string1; 
    } 
} 

function SecureString($value) { 
    $value = mysql_real_escape_string(stripslashes(strip_tags($value))); 
    return $value; 
} 
?> 
+3

這是一個不可能完成的任務,你可以趕上不好的話*(一個很長的字典)*,但你不能趕上這些所有變體詞:'FuUuCk','b1owJ0b' ...總之,嘗試這樣做只是浪費時間,並不能取代主持人。新聞:我們現在在2017年,您可以無風險地停止使用所有'mysql_'函數。 –

+0

我知道我不能阻止他們,這不是我的問題.... – noynac

+1

也許,但這是我的答案。 –

回答

1

代碼中很少有錯誤。請檢查工作示例如下:

<?php 
$str = "Kiss my badwordhere"; 
echo filter($str) . "\n"; 

$str = "badwordhere anywherebadword"; 
echo filter($str) . "\n"; 


function filter($string) { 
    $words = array("badwordhere", "anywherebadword"); 
    $new = array("****", "*******"); 
    $string1 = str_ireplace($words, $new, $string); 
    $left = preg_replace("/[\* ]+/", "", $string1); 
    if (strlen($left) == 0) { 
    $string1 = "[ Content Deleted ]"; 
    return $string1; 
    } 
    else { 
    return $string1; 
    } 
} 
?> 

打印

Kiss my **** 
[ Content Deleted ] 
+0

謝謝!!!! 偉大的作品:) – noynac

+0

剛剛改善處理這兩種情況。請通過檢查綠色標記來接受答案。 – MaxZoom