2010-02-02 78 views
3

任何人都知道解析用戶提交的評論的任何示例php(理想codeigniter)代碼。去除褻瀆和HTML標籤等?評論解析器的示例代碼

+0

當然,CodeIgniter有一個用於轉義HTML的簡單組件,對吧?字詞過濾器應該足夠小。 – Matchu 2010-02-02 00:20:37

回答

1

嘗試strip_tags擺脫提交的任何html。如果您只想確保註釋中不顯示任何html,則可以使用htmlspecialchars來轉義標記 - 按照Matchu的示例,使用它時會發生的意外影響要少於strip_tags。

對於一個字過濾器,取決於你想要去的深度,網上有很多例子,從simplecomplex。下面是傑克Olefsky的例子代碼(簡單的一個以前鏈接):

<? 
//This is totally free to use by anyone for any purpose. 

// BadWordFilter 
// This function does all the work. If $replace is 1 it will replace all bad words 
// with the wildcard replacements. If $replace is 0 it will not replace anything. 
// In either case, it will return 1 if it found bad words or 0 otherwise. 
// Be sure to fill the $bads array with the bad words you want filtered. 
function BadWordFilter(&$text, $replace) 
{ 
    //fill this array with the bad words you want to filter and their replacements 
    $bads = array (
     array("butt","b***"), 
     array("poop","p***"), 
     array("crap","c***") 
    ); 

    if($replace==1) {        //we are replacing 
     $remember = $text; 

     for($i=0;$i<sizeof($bads);$i++) {   //go through each bad word 
      $text = eregi_replace($bads[$i][0],$bads[$i][5],$text); //replace it 
     } 

     if($remember!=$text) return 1;    //if there are any changes, return 1 

    } else {          //we are just checking 

     for($i=0;$i<sizeof($bads);$i++) {   //go through each bad word 
      if(eregi($bads[$i][0],$text)) return 1; //if we find any, return 1 
     } 

    } 
} 

//this will replace all bad words with their replacements. $any is 1 if it found any 
$any = BadWordFilter($wordsToFilter,1); 

//this will not repace any bad words. $any is 1 if it found any 
$any = BadWordFilter($wordsToFilter,0); 

?> 

許多更多的例子可以found easily在網絡上。

+0

我改爲投票給htmlspecialchars,以確保沒有像「你好!」被誤認爲是HTML。 – Matchu 2010-02-02 00:24:07

+0

如果亞歷克斯想逃避他們,而不是刪除他們,那麼我同意,htmlspecialchars更好。 – jball 2010-02-02 00:26:07

+0

取決於什麼「刪除」的意思:) – Matchu 2010-02-02 00:32:36