PHP使用正則表達(dá)式實(shí)現(xiàn)過濾非法字符串功能示例
本文實(shí)例講述了PHP使用正則表達(dá)式實(shí)現(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>
二、運(yùn)行結(jié)果

發(fā)布主題:****
發(fā)布內(nèi)容:****客 ****包
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測(cè)試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php正則表達(dá)式用法總結(jié)》、《php程序設(shè)計(jì)安全教程》、《php安全過濾技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》及《php+mysql數(shù)據(jù)庫操作入門教程》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- Python使用正則表達(dá)式去除(過濾)HTML標(biāo)簽提取文字功能
- 利用Python正則表達(dá)式過濾敏感詞的方法
- 詳解Android過濾emoji表情正則表達(dá)式
- Python使用正則表達(dá)式過濾或替換HTML標(biāo)簽的方法詳解
- C#使用正則表達(dá)式過濾html標(biāo)簽
- JS使用正則表達(dá)式過濾多個(gè)詞語并替換為相同長(zhǎng)度星號(hào)的方法
- javascript用正則表達(dá)式過濾空格的實(shí)現(xiàn)代碼
- MYSQL使用正則表達(dá)式過濾數(shù)據(jù)
- Java正則表達(dá)式過濾出字母、數(shù)字和中文
- 使用正則表達(dá)式過濾 S3 上以 _$folder$ 結(jié)尾的占位文件的方法
相關(guān)文章
PHP中通過HTTP_USER_AGENT判斷是否為手機(jī)移動(dòng)終端的函數(shù)代碼
經(jīng)常我們需要做一些判斷是否是手機(jī)訪問的時(shí)候,然后進(jìn)行自動(dòng)跳轉(zhuǎn)操作,這是從一個(gè)PHP框架分離出來的一段判斷是否為手機(jī)移動(dòng)終端的函數(shù),分享下2013-02-02
PHP開發(fā)API接口簽名生成及驗(yàn)證操作示例
這篇文章主要介紹了PHP開發(fā)API接口簽名生成及驗(yàn)證操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了PHP開發(fā)API接口簽名生成及驗(yàn)證操作相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
PHP實(shí)現(xiàn)多維數(shù)組轉(zhuǎn)字符串和多維數(shù)組轉(zhuǎn)一維數(shù)組的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)多維數(shù)組轉(zhuǎn)字符串和多維數(shù)組轉(zhuǎn)一維數(shù)組的方法,以實(shí)例形式分別介紹了php數(shù)組與字符串的轉(zhuǎn)換技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08
有關(guān)于PHP中常見數(shù)據(jù)類型的匯總分享
本文整理了有關(guān)于PHP中常見的數(shù)據(jù)類型,感興趣的朋友可以參考下2014-01-01
PHP實(shí)現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的抓取小說網(wǎng)站內(nèi)容功能,涉及php頁面抓取、正則匹配、文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
淺析php如何實(shí)現(xiàn)爬取數(shù)據(jù)原理
在本篇文章中,小編給大家分享了關(guān)于php如何實(shí)現(xiàn)爬取數(shù)據(jù)的原理知識(shí)點(diǎn),有興趣的朋友們參考下。2018-09-09

