PHP使用正则表达式实现过滤非法字符串功能示例

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP使用正则表达式实现过滤非法字符串功能。分享给大家供大家参考,具体如下:

一、代码

1、index.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>
<style type="text/css">
<!--
body {
  margin-left: 0px;
  margin-top: 0px;
  margin-right: 0px;
  margin-bottom: 0px;
}
-->
</style></head>
<body>
<table width="1002" height="585" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td width="379" height="226"> </td>
  <td width="445"> </td>
  <td width="178"> </td>
 </tr>
   <form id="form1" name="form1" method="post" action="index_ok.php">
 <tr>
  <td height="260"> </td>
  <td align="center" valign="top"><table width="430" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#99CC67">
   <tr>
    <td width="81" height="30" align="right" bgcolor="#FFFFFF">发布主题:</td>
    <td width="307" align="left" bgcolor="#FFFFFF"><input name="title" type="text" id="title" size="30" /></td>
   </tr>
   <tr>
    <td align="right" bgcolor="#FFFFFF">发布内容:</td>
    <td align="left" bgcolor="#FFFFFF"><textarea name="content" cols="43" rows="13" id="content"></textarea></td>
   </tr>
  </table></td>
  <td> </td>
 </tr>
 <tr>
  <td height="99"> </td>
  <td align="center" valign="top"><table width="315" height="37" border="0" cellpadding="0" cellspacing="0">
   <tr>
    <td width="169" align="center"><input type="image" name="imageField" src="images/bg1.JPG" /></td>
    <td width="146" align="center"><input type="image" name="imageField2" src="images/bg3.JPG" onclick="form.reset();return false;" /></td>
   </tr>
  </table></td>
  <td> </td>
 </tr>
   </form>
</table>
</body>
</html>

2、index_ok.php

<?php
$title=$_POST[title];
$content=$_POST[content];
$str="****";
$titles = preg_replace("/(黑客)|(抓包)|(监听)/",$str,$title);
$contents = preg_replace("/(黑客)|(抓包)|(监听)/",$str,$content);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>过滤留言板中的非法字符</title>
<style type="text/css">
<!--
body {
  margin-left: 0px;
  margin-top: 0px;
  margin-right: 0px;
  margin-bottom: 0px;
}
.STYLE1 {
  font-size: 12px;
  color: #855201;
}
-->
</style></head>
<body>
<table width="1002" height="585" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td width="400" height="226"> </td>
  <td width="406"> </td>
  <td width="196"> </td>
 </tr>
   <form id="form1" name="form1" method="post" action="index_ok.php">
 <tr>
  <td height="260"> </td>
  <td align="left" valign="top"><p class="STYLE1">发布主题:<?php echo $titles;?></p>
   <p class="STYLE1">发布内容:<?php echo $contents;?></p></td>
  <td> </td>
 </tr>
 <tr>
  <td> </td>
  <td align="center" valign="top"> </td>
  <td> </td>
 </tr>
 </form>
</table>
</body>
</html>

二、运行结果

发布主题:****
发布内容:****客 ****包

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《php程序设计安全教程》、《php安全过滤技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》及《php+mysql数据库操作入门教程

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php的SimpleXML方法读写XML接口文件实例解析

在php5中读写xml文档是非常方便的,可以直接使用php的SimpleXML方法来快速解析与生成xml格式的文件,下面举例说明: 创建一个SimpleXML对象有三种方法: 1.使用n...

php实例化一个类的具体方法

类的实例化就是对象。一个类可以分成两个部分,一个是静态描述,就是类里的成员属性。第二个是动态描述,就是类里的成员方法,也就是对象的功能。 声明一个类,可以在class前加一些关键字,如a...

PHP获取链表中倒数第K个节点的方法

本文实例讲述了PHP获取链表中倒数第K个节点的方法。分享给大家供大家参考,具体如下: 问题 输入一个链表,输出该链表中倒数第k个结点。 解决思路 注意这个题目是返回节点,而不是返回值。返...

详解PHP的Yii框架中的Controller控制器

控制器是 MVC 模式中的一部分, 是继承yii\base\Controller类的对象,负责处理请求和生成响应。 具体来说,控制器从应用主体接管控制后会分析请求数据并传送到模型, 传送...

php循环语句 for()与foreach()用法区别介绍

for 循环是 PHP 中最复杂的循环结构。它的行为和 C 语言的相似。 for 循环的语法是: for (expr1; expr2; expr3) statement 第一个表达式(e...