溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在PHP中利用正則表達(dá)式過濾非法字符串

發(fā)布時間:2021-04-07 17:41:40 來源:億速云 閱讀:164 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹如何在PHP中利用正則表達(dá)式過濾非法字符串,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一、代碼

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">發(fā)布主題:</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">發(fā)布內(nèi)容:</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("/(黑客)|(抓包)|(監(jiān)聽)/",$str,$title);
$contents = preg_replace("/(黑客)|(抓包)|(監(jiān)聽)/",$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">發(fā)布主題:<?php echo $titles;?></p>
   <p class="STYLE1">發(fā)布內(nèi)容:<?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>

二、運行結(jié)果

如何在PHP中利用正則表達(dá)式過濾非法字符串

發(fā)布主題:****
發(fā)布內(nèi)容:****客 ****包

關(guān)于如何在PHP中利用正則表達(dá)式過濾非法字符串就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI