gpt4 book ai didi

php - 评论解析器的示例代码

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:43 25 4
gpt4 key购买 nike

任何人都知道用于解析用户提交的评论的任何示例 php(最好是 codeigniter)代码。要删除脏话和 HTML 标签等?

最佳答案

尝试 strip_tags摆脱任何提交的 html。您可以使用 htmlspecialchars如果您只是想确保评论中不显示任何 html,则转义标签 - 根据 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在网络上。

关于php - 评论解析器的示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2181055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com