注释解析器的示例代码

时间:2010-02-02 00:19:07

标签: php codeigniter

任何人都知道用于解析用户提交的评论的任何示例php(理想情况下是codeigniter)代码。删除亵渎和HTML标签等?

1 个答案:

答案 0 :(得分:1)

尝试strip_tags删除任何提交的HTML。如果你只是想确保评论中没有显示html,你可以使用htmlspecialchars来转义标签 - 根据Matchu的例子,使用它会比使用strip_tags更少意外的效果。

对于字词过滤器,根据您想要的深度,网上有很多例子,从simplecomplex。这是Jake 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