PHP使用正則表達式實現(xiàn)過濾非法字符串功能示例
本文實例講述了PHP使用正則表達式實現(xiàn)過濾非法字符串功能。分享給大家供大家參考,具體如下:
一、代碼
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">發(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> </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("/(黑客)|(抓包)|(監(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"> </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">發(fā)布主題:<?php echo $titles;?></p>
<p class="STYLE1">發(fā)布內(nèi)容:<?php echo $contents;?></p></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="center" valign="top"> </td>
<td> </td>
</tr>
</form>
</table>
</body>
</html>
二、運行結(jié)果

發(fā)布主題:****
發(fā)布內(nèi)容:****客 ****包
PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php正則表達式用法總結(jié)》、《php程序設計安全教程》、《php安全過濾技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》及《php+mysql數(shù)據(jù)庫操作入門教程》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP類的聲明與實例化及構(gòu)造方法與析構(gòu)方法詳解
這篇文章主要介紹了PHP類的聲明與實例化及構(gòu)造方法與析構(gòu)方法,結(jié)合實例形式分析了PHP面向?qū)ο箢惖穆暶髋c使用相關技巧,需要的朋友可以參考下2016-01-01
推薦學習php sesson的朋友必看PHP會話(Session)使用入門
對比起 Cookie,Session 是存儲在服務器端的會話,相對安全,并且不像 Cookie 那樣有存儲長度限制,本文簡單介紹 Session 的使用。2008-08-08
JS+PHP實現(xiàn)用戶輸入數(shù)字后顯示最大的值及所在位置
這篇文章主要給大家介紹了JS+PHP實現(xiàn)用戶輸入數(shù)字后顯示最大的值及是第幾個的相關位置,文中給出了詳細的示例代碼供大家參考學習,需要的朋友們下面來一起看看吧。2017-06-06
php 使用curl模擬ip和來源進行訪問的實現(xiàn)方法
下面小編就為大家?guī)硪黄猵hp 使用curl模擬ip和來源進行訪問的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Php Image Resize圖片大小調(diào)整的函數(shù)代碼
php下可以將圖片大小調(diào)整的函數(shù)代碼,需要的朋友可以參考下。2011-01-01

