Home  >  Article  >  Backend Development  >  Example of PHP using regular expressions to filter illegal strings

Example of PHP using regular expressions to filter illegal strings

不言
不言Original
2018-06-05 10:39:122017browse

This article mainly introduces PHP's use of regular expressions to implement the function of filtering illegal strings. Combined with the example of the message board data submission function, it analyzes the related operation skills of PHP using the regular expression preg_replace function to perform regular string replacement. Friends who need it can Refer to the example below

This article describes how PHP uses regular expressions to implement the function of filtering illegal strings. Share it with everyone for your reference, the details are as follows:

1. Code

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>

2. Running results

Post topic: ****
Post content: **** customer **** package

Related recommendations:

Long text paging display function implemented in PHP

##

The above is the detailed content of Example of PHP using regular expressions to filter illegal strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php delegation modeNext article:php delegation mode