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

发布时间:2021-04-07 17:41:40 作者:Leah
来源:亿速云 阅读:149

这篇文章给大家介绍如何在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">&nbsp;</td>
  <td width="445">&nbsp;</td>
  <td width="178">&nbsp;</td>
 </tr>
   <form id="form1" name="form1" method="post" action="index_ok.php">
 <tr>
  <td height="260">&nbsp;</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>&nbsp;</td>
 </tr>
 <tr>
  <td height="99">&nbsp;</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>&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>
<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">&nbsp;</td>
  <td width="406">&nbsp;</td>
  <td width="196">&nbsp;</td>
 </tr>
   <form id="form1" name="form1" method="post" action="index_ok.php">
 <tr>
  <td height="260">&nbsp;</td>
  <td align="left" valign="top"><p class="STYLE1">发布主题:<?php echo $titles;?></p>
   <p class="STYLE1">发布内容:<?php echo $contents;?></p></td>
  <td>&nbsp;</td>
 </tr>
 <tr>
  <td>&nbsp;</td>
  <td align="center" valign="top">&nbsp;</td>
  <td>&nbsp;</td>
 </tr>
 </form>
</table>
</body>
</html>

二、运行结果

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

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

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

推荐阅读:
  1. 如何在php中做sql过滤
  2. Python sql注入 过滤字符串的非法字符实例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 正则表达式 字符串

上一篇:如何在python中使用requests上传多个文件

下一篇:怎么在CSS3中利用transition属性实现下划线

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》