您好,欢迎访问一九零五行业门户网

PHP实现过滤留言信息中的敏感词

php实现过滤留言信息中的敏感词
一 代码
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>检测留言信息的敏感词</title> </head> <body> <table width="761" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#fefefe" bgcolor="#ffffff"> <form action="" method="post" name="form1" id="form1"> <tr> <td width="761" align="center" bgcolor="#f9f8ef"><table width="749" border="0" align="center" cellpadding="0" cellspacing="0" style="border-collapse: collapse"> <tr> <td height="36" colspan="3" align="left" " bgcolor="#f9f8ef" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;姓&nbsp;&nbsp;名: <input name="user_name" id="user_name" value=" 匿名" maxlength="64" type="text" /> <span style="color: #ff0000">*</span></td> </tr> <tr> <td height="36" colspan="3" align="left" bgcolor="#f9f8ef">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;标&nbsp;&nbsp;题: <input maxlength="64" size="30" name="title" type="text"/> <span style="color: #ff0000">*</span></td> </tr> <tr> <td height="126" colspan="3" align="left" bgcolor="#f9f8ef">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;内&nbsp;&nbsp;容: <textarea name="content" cols="60" rows="8" id="content" ></textarea> <span style="color: #ff0000">*</span></td> </tr> <tr> <td height="40" colspan="3" align="center" bgcolor="#f9f8ef"><input name="submit" type="submit" class="btn1" id="submit" value="签写留言"/> &nbsp;&nbsp; <input name="reset" type="reset" class="btn1" value="清除留言" /></td> </tr> </table></td> </tr> <tr> <td width="761" align="center" bgcolor="#f9f8ef"><table width="749" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="703" height="31" align="center" >&nbsp;</td> </tr> </table></td> </tr> </form> </table> </body> </html> <?php if($_post){ if (is_file("./filterwords.txt")){//判断给定文件名是否为一个正常的文件 $filter_word = file("./filterwords.txt");//把整个文件读入一个数组中 $str=$_post['content']; for($i=0;$i<count($filter_word);$i++){//应用for循环语句对敏感词进行判断 if(preg_match("/".trim($filter_word[$i])."/i",$str)){//应用正则表达式,判断传递的留言信息中是否含有敏感词 echo "<script> alert('留言信息中包含敏感词!');history.back(-1);</script>"; exit; } } } } ?>
二 运行结果
三 代码分析
当用户提交留言信息时,程序会通过正则表达式preg_mctch()函数来实现将留言信息与存储在数组中的敏感词进行对比,检验用户提交的留言是否含有敏感词。如果留言中含有敏感词,那么将弹出提示信息,否则留言信息发布成功。
preg_match()函数中的“/i”是指在进行敏感词汇比较时不区分字母大小写。
以上就是php实现过滤留言信息中的敏感词的内容。
相关文章:
一个高效的敏感词过滤方法(php)
利用php扩展trie_filter做中文敏感词过滤
php敏感词过滤使用第三方扩展trie_filter
其它类似信息

推荐信息