如何在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">&nbsp;</td>
<tdwidth="445">&nbsp;</td>
<tdwidth="178">&nbsp;</td>
</tr>
<formid="form1"name="form1"method="post"action="index_ok.php">
<tr>
<tdheight="260">&nbsp;</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>&nbsp;</td>
</tr>
<tr>
<tdheight="99">&nbsp;</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>&nbsp;</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">&nbsp;</td>
<tdwidth="406">&nbsp;</td>
<tdwidth="196">&nbsp;</td>
</tr>
<formid="form1"name="form1"method="post"action="index_ok.php">
<tr>
<tdheight="260">&nbsp;</td>
<tdalign="left"valign="top"><pclass="STYLE1">发布主题:<?phpecho$titles;?></p>
<pclass="STYLE1">发布内容:<?phpecho$contents;?></p></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<tdalign="center"valign="top">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</form>
</table>
</body>
</html>

二、运行结果

如何在PHP中利用正则表达式过滤非法字符串

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

关于如何在PHP中利用正则表达式过滤非法字符串就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

发布于 2021-04-08 13:38:07
收藏
分享
海报
0 条评论
164
上一篇:怎么在d3.js中自定义一个多y轴折线图 下一篇:如何在python中使用requests上传多个文件
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码