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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php正則表達式用法總結(jié)》、《php程序設(shè)計安全教程》、《php安全過濾技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》及《php+mysql數(shù)據(jù)庫操作入門教程》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP中通過HTTP_USER_AGENT判斷是否為手機移動終端的函數(shù)代碼
經(jīng)常我們需要做一些判斷是否是手機訪問的時候,然后進行自動跳轉(zhuǎn)操作,這是從一個PHP框架分離出來的一段判斷是否為手機移動終端的函數(shù),分享下2013-02-02
PHP實現(xiàn)多維數(shù)組轉(zhuǎn)字符串和多維數(shù)組轉(zhuǎn)一維數(shù)組的方法
這篇文章主要介紹了PHP實現(xiàn)多維數(shù)組轉(zhuǎn)字符串和多維數(shù)組轉(zhuǎn)一維數(shù)組的方法,以實例形式分別介紹了php數(shù)組與字符串的轉(zhuǎn)換技巧,非常簡單實用,需要的朋友可以參考下2015-08-08
有關(guān)于PHP中常見數(shù)據(jù)類型的匯總分享
本文整理了有關(guān)于PHP中常見的數(shù)據(jù)類型,感興趣的朋友可以參考下2014-01-01
PHP實現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能示例
這篇文章主要介紹了PHP實現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能,涉及php頁面抓取、正則匹配、文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
淺析php如何實現(xiàn)爬取數(shù)據(jù)原理
在本篇文章中,小編給大家分享了關(guān)于php如何實現(xiàn)爬取數(shù)據(jù)的原理知識點,有興趣的朋友們參考下。2018-09-09

