本文实例讲述了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="/UploadFiles/2021-04-02/bg1.JPG">

2、index_ok.php

<"****";
$titles = preg_replace("/(黑客)|(抓包)|(监听)/",$str,$title);
$contents = preg_replace("/(黑客)|(抓包)|(监听)/",$str,$content);
"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">发布主题:<"STYLE1">发布内容:<"center" valign="top">&nbsp;</td>
  <td>&nbsp;</td>
 </tr>
 </form>
</table>
</body>
</html>

二、运行结果

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

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

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程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。