2010-02-02 8 views
3

사용자가 제출 한 주석을 구문 분석하기위한 샘플 php (이상 코드 서명자) 코드를 알고 있습니다. 욕설과 HTML 태그 등을 제거 하시겠습니까?주석 파서의 예제 코드

+0

확실히 CodeIgniter에는 HTML을 이스케이프하기위한 쉬운 구성 요소가 있습니다. 맞습니까? 단어 필터는 아주 사소해야합니다. – Matchu

답변

1

제출 된 HTML을 제거하려면 strip_tags을 시도하십시오. 댓글에 html이 표시되지 않도록하려면 htmlspecialchars을 사용하면됩니다. Matchu의 예에 따르면 strip_tags보다 의도하지 않은 영향이 적습니다.

단어 필터의 경우, 원하는 단어의 깊이에 따라 simple에서 complex까지 많은 예제가 웹에 있습니다. 이것의

<? 
//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 수 있습니다 : 여기 제이크 Olefsky의 예에서 코드 (이전에 링크 간단한 하나)입니다.

+0

대신 htmlspecialchars에 투표하여 "Hello !"과 같은 것을 확인합니다. HTML과 오인됩니다. – Matchu

+0

Alex가 그들을 탈출하고 제거하지 않으려한다면, 나는 동의한다. htmlspecialchars가 더 좋다. – jball

+0

"제거"는 무엇을 의미 하는가에 따라 달라집니다. – Matchu