php如何清除html代碼

這篇“php如何清除html代碼”文章,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要參考一下,對(duì)于“php如何清除html代碼”,小編整理了以下知識(shí)點(diǎn),請(qǐng)大家跟著小編的步伐一步一步的慢慢理解,接下來就讓我們進(jìn)入主題吧。

房山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

php有什么用

php是一個(gè)嵌套的縮寫名稱,指的是英文超級(jí)文本預(yù)處理語言(php:Hypertext Preprocessor)的縮寫,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。

php清除html代碼的方法:1、通過“strip_tags($str)”去掉HTML及PHP的標(biāo)記;2、通過htmlspecialchars把html中的標(biāo)簽轉(zhuǎn)換為html實(shí)體;3、通過正則表達(dá)式過濾css等樣式。

php中去除文字內(nèi)容中所有html代碼

PHP已經(jīng)為我們提供了很多清除html格式的方法了,下面就讓老高介紹一下。

I. strip_tags

strip_tags($str) 去掉 HTML 及 PHP 的標(biāo)記

語法: string strip_tags(string str);

傳回值: 字串

函式種類: 資料處理

內(nèi)容說明 :

解析:本函式可去掉字串中包含的任何 HTML 及 PHP 的標(biāo)記字串。若是字串的 HTML 及 PHP 標(biāo)簽原來就有錯(cuò),例如少了大于的符號(hào),則也會(huì)傳回錯(cuò)誤。這個(gè)函數(shù)和 fgetss() 有著相同的功能

例子

echo strip_tags("Hello world!");
# Hello world!

II. htmlspecialchars

這個(gè)函數(shù)把html中的標(biāo)簽轉(zhuǎn)換為html實(shí)體,博客的代碼展示就必須使用這個(gè)函數(shù),要不貼出來的代碼就會(huì)被執(zhí)行了。

預(yù)定義的字符是:

& (和號(hào)) 成為 &
” (雙引號(hào)) 成為 ”
‘ (單引號(hào)) 成為 ‘
< (小于) 成為 < > (大于) 成為 >

例子

$new = htmlspecialchars("Test", ENT_QUOTES);
echo $new; 
# <a href='test'>Test</a>
# 如果需要展現(xiàn)
,那么瀏覽器解析HTML的時(shí)候會(huì)自動(dòng)將他變?yōu)閾Q行
# 但是通過htmlspecialchars就可以讓< 變?yōu)?nbsp;'

與htmlspecialchars功能相反的函數(shù)是htmlspecialchars_decode,他會(huì)把HTML實(shí)體轉(zhuǎn)化為字符!

III. 后補(bǔ)函數(shù)

PHP去除html、css樣式、js格式的方法很多,但發(fā)現(xiàn),它們基本都有一個(gè)弊端:空格往往清除不了

經(jīng)過不斷的研究,最終找到了一個(gè)理想的去除html包括空格css樣式、js 的PHP函數(shù)。

$descclear = str_replace("\r","",$descclear);//過濾換行
$descclear = str_replace("\n","",$descclear);//過濾換行
$descclear = str_replace("\t","",$descclear);//過濾換行
$descclear = str_replace("\r\n","",$descclear);//過濾換行
$descclear = preg_replace("/\s+/", " ", $descclear);//過濾多余回車
$descclear = preg_replace("/<[ ]+/si","<",$descclear); //過濾<__("<"號(hào)后面帶空格)
$descclear = preg_replace("/<\!--.*?-->/si","",$descclear); //過濾html注釋
$descclear = preg_replace("/<(\!.*?)>/si","",$descclear); //過濾DOCTYPE
$descclear = preg_replace("/<(\/?html.*?)>/si","",$descclear); //過濾html標(biāo)簽
$descclear = preg_replace("/<(\/?head.*?)>/si","",$descclear); //過濾head標(biāo)簽
$descclear = preg_replace("/<(\/?meta.*?)>/si","",$descclear); //過濾meta標(biāo)簽
$descclear = preg_replace("/<(\/?body.*?)>/si","",$descclear); //過濾body標(biāo)簽
$descclear = preg_replace("/<(\/?link.*?)>/si","",$descclear); //過濾link標(biāo)簽
$descclear = preg_replace("/<(\/?form.*?)>/si","",$descclear); //過濾form標(biāo)簽
$descclear = preg_replace("/cookie/si","COOKIE",$descclear); //過濾COOKIE標(biāo)簽
$descclear = preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$descclear); //過濾applet標(biāo)簽
$descclear = preg_replace("/<(\/?applet.*?)>/si","",$descclear); //過濾applet標(biāo)簽
$descclear = preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$descclear); //過濾style標(biāo)簽
$descclear = preg_replace("/<(\/?style.*?)>/si","",$descclear); //過濾style標(biāo)簽
$descclear = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$descclear); //過濾title標(biāo)簽
$descclear = preg_replace("/<(\/?title.*?)>/si","",$descclear); //過濾title標(biāo)簽
$descclear = preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$descclear); //過濾object標(biāo)簽
$descclear = preg_replace("/<(\/?objec.*?)>/si","",$descclear); //過濾object標(biāo)簽
$descclear = preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$descclear); //過濾noframes標(biāo)簽
$descclear = preg_replace("/<(\/?noframes.*?)>/si","",$descclear); //過濾noframes標(biāo)簽
$descclear = preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$descclear); //過濾frame標(biāo)簽
$descclear = preg_replace("/<(\/?i?frame.*?)>/si","",$descclear); //過濾frame標(biāo)簽
$descclear = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$descclear); //過濾script標(biāo)簽
$descclear = preg_replace("/<(\/?script.*?)>/si","",$descclear); //過濾script標(biāo)簽
$descclear = preg_replace("/javascript/si","Javascript",$descclear); //過濾script標(biāo)簽
$descclear = preg_replace("/vbscript/si","Vbscript",$descclear); //過濾script標(biāo)簽
$descclear = preg_replace("/on([a-z]+)\s*=/si","On\\1=",$descclear); //過濾script標(biāo)簽
$descclear = preg_replace("/

以上是“php如何清除html代碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享標(biāo)題:php如何清除html代碼
鏈接地址:http://bm7419.com/article38/jcissp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站制作、營銷型網(wǎng)站建設(shè)虛擬主機(jī)、網(wǎng)站營銷、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司