PHP使用正則表達(dá)式實現(xiàn)過濾非法字符串功能示例
本文實例講述了PHP使用正則表達(dá)式實現(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款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
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è)計安全教程》、《php安全過濾技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》及《php+mysql數(shù)據(jù)庫操作入門教程》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- Python使用正則表達(dá)式去除(過濾)HTML標(biāo)簽提取文字功能
- 利用Python正則表達(dá)式過濾敏感詞的方法
- 詳解Android過濾emoji表情正則表達(dá)式
- Python使用正則表達(dá)式過濾或替換HTML標(biāo)簽的方法詳解
- C#使用正則表達(dá)式過濾html標(biāo)簽
- JS使用正則表達(dá)式過濾多個詞語并替換為相同長度星號的方法
- javascript用正則表達(dá)式過濾空格的實現(xiàn)代碼
- MYSQL使用正則表達(dá)式過濾數(shù)據(jù)
- Java正則表達(dá)式過濾出字母、數(shù)字和中文
- 使用正則表達(dá)式過濾 S3 上以 _$folder$ 結(jié)尾的占位文件的方法
相關(guān)文章
PHP類的聲明與實例化及構(gòu)造方法與析構(gòu)方法詳解
這篇文章主要介紹了PHP類的聲明與實例化及構(gòu)造方法與析構(gòu)方法,結(jié)合實例形式分析了PHP面向?qū)ο箢惖穆暶髋c使用相關(guān)技巧,需要的朋友可以參考下2016-01-01推薦學(xué)習(xí)php sesson的朋友必看PHP會話(Session)使用入門
對比起 Cookie,Session 是存儲在服務(wù)器端的會話,相對安全,并且不像 Cookie 那樣有存儲長度限制,本文簡單介紹 Session 的使用。2008-08-08PHP學(xué)習(xí)筆記之三 數(shù)據(jù)庫基本操作
本文介紹最基本最實用的數(shù)據(jù)庫操作。首先簡單復(fù)習(xí)下MySQL的使用方法,并且建好一張表備用。MySQL在Linux系統(tǒng)上一般都是裝好的,在win下安裝也很簡單,不多做介紹。2011-01-01JS+PHP實現(xiàn)用戶輸入數(shù)字后顯示最大的值及所在位置
這篇文章主要給大家介紹了JS+PHP實現(xiàn)用戶輸入數(shù)字后顯示最大的值及是第幾個的相關(guān)位置,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-06-06php 使用curl模擬ip和來源進(jìn)行訪問的實現(xiàn)方法
下面小編就為大家?guī)硪黄猵hp 使用curl模擬ip和來源進(jìn)行訪問的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Php Image Resize圖片大小調(diào)整的函數(shù)代碼
php下可以將圖片大小調(diào)整的函數(shù)代碼,需要的朋友可以參考下。2011-01-01