佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > PHP使用正則表達式實現(xiàn)過濾非法字符串功能示例

PHP使用正則表達式實現(xiàn)過濾非法字符串功能示例

熱門標簽:電子圍欄 科大訊飛語音識別系統(tǒng) Linux服務(wù)器 團購網(wǎng)站 銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置 服務(wù)器配置 阿里云

本文實例講述了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">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é)果

發(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è)計有所幫助。

您可能感興趣的文章:
  • PHP常用正則表達式精選(推薦)
  • PHP正則表達式筆記與實例詳解
  • PHP利用正則表達式實現(xiàn)手機號碼中間4位用星號(*)替換顯示功能
  • 史上最全的PHP正則表達式(手機號需要加上177-***)
  • PHP preg_match實現(xiàn)正則表達式匹配功能【輸出是否匹配及匹配值】
  • PHP手機號碼及郵箱正則表達式實例解析

標簽:衢州 廣元 萍鄉(xiāng) 大理 蚌埠 江蘇 衡水 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標題《PHP使用正則表達式實現(xiàn)過濾非法字符串功能示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    正蓝旗| 申扎县| 盘山县| 德江县| 车险| 洪泽县| 谢通门县| 桃源县| 满洲里市| 尼木县| 太仆寺旗| 屏山县| 札达县| 南涧| 满洲里市| 中阳县| 缙云县| 潜山县| 潼关县| 梨树县| 博爱县| 德清县| 天津市| 台南市| 原平市| 滦平县| 柞水县| 镇雄县| 商河县| 广宗县| 开阳县| 寻甸| 万山特区| 林西县| 茂名市| 墨玉县| 肥东县| 吉安县| 鲁山县| 平原县| 兖州市|