登录  /  注册

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

巴扎黑
发布: 2017-03-22 14:29:01
原创
3272人浏览过

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" >        姓  名:
            <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">        标  题:
            <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">        内  容:
            <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="签写留言"/>
  
<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" > </td>
            </tr>
        </table></td>
    </tr>
  </form>
</table>
</body>
</html>
<?php
if($_POST){
if (is_file("./filterwords.txt")){//判断给定文件名是否为一个正常的文件
   $filter_word = file("./filterwords.txt");//把整个文件读入一个数组中
$str=$_POST[&#39;content&#39;];
for($i=0;$i<count($filter_word);$i++){//应用For循环语句对敏感词进行判断
  if(preg_match("/".trim($filter_word[$i])."/i",$str)){//应用正则表达式,判断传递的留言信息中是否含有敏感词
 echo "<script> alert(&#39;留言信息中包含敏感词!&#39;);history.back(-1);</script>";
 exit;
}
}
}
}
?>
登录后复制

二 运行结果

69fe2f6f-2395-3604-a5fa-5e24722a3411.png

三 代码分析

当用户提交留言信息时,程序会通过正则表达式preg_mctch()函数来实现将留言信息与存储在数组中的敏感词进行对比,检验用户提交的留言是否含有敏感词。如果留言中含有敏感词,那么将弹出提示信息,否则留言信息发布成功。

 preg_match()函数中的“/i”是指在进行敏感词汇比较时不区分字母大小写。

以上就是PHP实现过滤留言信息中的敏感词的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关文章:

一个高效的敏感词过滤方法(PHP)

利用PHP扩展trie_filter做中文敏感词过滤

php敏感词过滤使用第三方扩展trie_filter

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号