如何在PHP中利用正则表达式过滤非法字符串
作者
这篇文章给大家介绍如何在PHP中利用正则表达式过滤非法字符串,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
一、代码
1、index.php
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/> <title>过滤留言板中的非法字符</title> <styletype="text/css"> <!-- body{ margin-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; } --> </style></head> <body> <tablewidth="1002"height="585"border="0"align="center"cellpadding="0"cellspacing="0"> <tr> <tdwidth="379"height="226"> </td> <tdwidth="445"> </td> <tdwidth="178"> </td> </tr> <formid="form1"name="form1"method="post"action="index_ok.php"> <tr> <tdheight="260"> </td> <tdalign="center"valign="top"><tablewidth="430"border="1"cellpadding="1"cellspacing="1"bordercolor="#FFFFFF"bgcolor="#99CC67"> <tr> <tdwidth="81"height="30"align="right"bgcolor="#FFFFFF">发布主题:</td> <tdwidth="307"align="left"bgcolor="#FFFFFF"><inputname="title"type="text"id="title"size="30"/></td> </tr> <tr> <tdalign="right"bgcolor="#FFFFFF">发布内容:</td> <tdalign="left"bgcolor="#FFFFFF"><textareaname="content"cols="43"rows="13"id="content"></textarea></td> </tr> </table></td> <td> </td> </tr> <tr> <tdheight="99"> </td> <tdalign="center"valign="top"><tablewidth="315"height="37"border="0"cellpadding="0"cellspacing="0"> <tr> <tdwidth="169"align="center"><inputtype="image"name="imageField"src="images/bg1.JPG"/></td> <tdwidth="146"align="center"><inputtype="image"name="imageField2"src="images/bg3.JPG"onclick="form.reset();returnfalse;"/></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> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/> <title>过滤留言板中的非法字符</title> <styletype="text/css"> <!-- body{ margin-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; } .STYLE1{ font-size:12px; color:#855201; } --> </style></head> <body> <tablewidth="1002"height="585"border="0"align="center"cellpadding="0"cellspacing="0"> <tr> <tdwidth="400"height="226"> </td> <tdwidth="406"> </td> <tdwidth="196"> </td> </tr> <formid="form1"name="form1"method="post"action="index_ok.php"> <tr> <tdheight="260"> </td> <tdalign="left"valign="top"><pclass="STYLE1">发布主题:<?phpecho$titles;?></p> <pclass="STYLE1">发布内容:<?phpecho$contents;?></p></td> <td> </td> </tr> <tr> <td> </td> <tdalign="center"valign="top"> </td> <td> </td> </tr> </form> </table> </body> </html>
二、运行结果
发布主题:****发布内容:****客 ****包
关于如何在PHP中利用正则表达式过滤非法字符串就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
目录
推荐阅读
-
php字符串增加1如何实现
php字符串增加1如何实现这篇“php字符串增加1如何实现”文章的...
-
php如何判断字符串是否有中文
-
php如何实现字符串去掉头尾
-
php字符串的组成是什么
php字符串的组成是什么这篇文章主要讲解了“php字符串的组成是什...
-
php如何让Swoole/Pool进程池实现Redis持久连接
php如何让Swoole/Pool进程池实现Redis持久连接本篇...
-
php字符串长度不一致如何解决
-
php时区不正确如何解决
-
php+fread()乱码如何解决
php+fread()乱码如何解决本篇内容介绍了“php+frea...
-
php explode报错如何解决
-
linux Centos如何安装PHP7
linuxCentos如何安装PHP7今天小编给大家分享一下li...
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~