PHP使用Regex實現過濾非法字串功能樣本

來源:互聯網
上載者:User
這篇文章主要介紹了PHP使用Regex實現過濾非法字串功能,結合留言板資料提交功能樣本分析了php使用Regexpreg_replace函數進行字串正則替換相關操作技巧,需要的朋友可以參考下

本文執行個體講述了PHP使用Regex實現過濾非法字串功能。分享給大家供大家參考,具體如下:

一、代碼

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>

二、運行結果

發布主題:****
發布內容:****客 ****包

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.