怎麼去除文字中的html標籤我要顯示成純文字

2021-06-26 08:11:18 字數 4697 閱讀 1195

1樓:

在網頁剛流行起來的時候,提取html中的文字有一個簡單的方法,就是將html文字(包含標記)中的所有以「<」符號開頭到以「>」符號之間的內容去掉即可。

但對於現在複雜的網頁而言,用這種方法提取出來的文字會有大量的空格、空行、script段落、還有一些html轉義字元,效果很差。

下面用正規表示式來提取html中的文字,

**的實現的思路是:

a、先將html文字中的所有空格、換行符去掉(因為html中的空格和換行是被忽略的)

b、將)", string.empty, regexoptions.ignorecase);

result = regex.replace(result, @"<( )*script([^>])*>", ")", string.empty, regexoptions.

ignorecase);

//remove all styles

result = regex.replace(result, @"<( )*style([^>])*>", ")", string.empty, regexoptions.

ignorecase);

//insert tabs in spaces of tags

result = regex.replace(result, @"<( )*td([^>])*>", " ", regexoptions.ignorecase);

//insert line breaks in places of

and tags

result = regex.replace(result, @"<( )*br( )*>", "\r", regexoptions.ignorecase);

result = regex.replace(result, @"<( )*li( )*>", "\r", regexoptions.ignorecase);

//insert line paragraphs in places of and tags

result = regex.replace(result, @"<( )*tr([^>])*>", "\r\r", regexoptions.ignorecase);

result = regex.replace(result, @"<( )*p([^>])*>", "\r\r", regexoptions.ignorecase);

//remove anything thats enclosed inside < >

result = regex.replace(result, @"<[^>]*>", string.empty, regexoptions.ignorecase);

//replace special characters:

result = regex.replace(result, @"&", "&", regexoptions.ignorecase);

result = regex.replace(result, @" ", " ", regexoptions.ignorecase);

result = regex.replace(result, @"<", "<", regexoptions.ignorecase);

result = regex.replace(result, @">", ">", regexoptions.ignorecase);

result = regex.replace(result, @"&(.);", string.empty, regexoptions.ignorecase);

//remove extra line breaks and tabs

result = regex.replace(result, @" ( )+", " ");

result = regex.replace(result, "(\r)( )+(\r)", "\r\r");

result = regex.replace(result, @"(\r\r)+", "\r\n");

return result;

}}//end class

}//end namespace

2樓:匿名使用者

迴圈替換所有html標記,如果是程式設計就簡單一些,如果是手動的話就是機械操作

3樓:

一種方法

$str=preg_replace('~<.*?>~','',$str);

但這種可能會誤刪除 文字本來的<>

還有一種方法 用 php 自帶的函式

$str=strip_tags($str);

這個是自帶的函式 會有什麼意想不到的事情 就不清楚了

4樓:匿名使用者

直接拼到頁面上就行了

5樓:

可以註釋

//代表註釋一行 或是 /* */代表註釋一段

html 顯示純文字,標籤也顯示出來 15

6樓:郭某人來此

不知道這個用的著不!

在網頁剛流行起來的時候,提取html中的文字有一個簡單的方法,就是將html文字(包含標記)中的所有以「<」符號開頭到以「>」符號之間的內容去掉即可。

但對於現在複雜的網頁而言,用這種方法提取出來的文字會有大量的空格、空行、script段落、還有一些html轉義字元,效果很差。

下面用正規表示式來提取html中的文字,

**的實現的思路是:

a、先將html文字中的所有空格、換行符去掉(因為html中的空格和換行是被忽略的)

b、將)", string.empty, regexoptions.ignorecase);

result = regex.replace(result, @"<( )*script([^>])*>", ")", string.empty, regexoptions.

ignorecase);

//remove all styles

result = regex.replace(result, @"<( )*style([^>])*>", ")", string.empty, regexoptions.

ignorecase);

//insert tabs in spaces of tags

result = regex.replace(result, @"<( )*td([^>])*>", " ", regexoptions.ignorecase);

//insert line breaks in places of

and tags

result = regex.replace(result, @"<( )*br( )*>", "\r", regexoptions.ignorecase);

result = regex.replace(result, @"<( )*li( )*>", "\r", regexoptions.ignorecase);

//insert line paragraphs in places of and tags

result = regex.replace(result, @"<( )*tr([^>])*>", "\r\r", regexoptions.ignorecase);

result = regex.replace(result, @"<( )*p([^>])*>", "\r\r", regexoptions.ignorecase);

//remove anything thats enclosed inside < >

result = regex.replace(result, @"<[^>]*>", string.empty, regexoptions.ignorecase);

//replace special characters:

result = regex.replace(result, @"&", "&", regexoptions.ignorecase);

result = regex.replace(result, @" ", " ", regexoptions.ignorecase);

result = regex.replace(result, @"<", "<", regexoptions.ignorecase);

result = regex.replace(result, @">", ">", regexoptions.ignorecase);

result = regex.replace(result, @"&(.);", string.empty, regexoptions.ignorecase);

//remove extra line breaks and tabs

result = regex.replace(result, @" ( )+", " ");

result = regex.replace(result, "(\r)( )+(\r)", "\r\r");

result = regex.replace(result, @"(\r\r)+", "\r\n");

return result;

}}//end class

}//end namespace

7樓:匿名使用者

因為html解析是< 和 >這兩個尖括號,所以不管你用什麼方法帶尖括號的都是顯示不出來的,但是隻要html頁面載入時找不到<,>符號就可以用,但是實現不了你的需求。

舉例:不可識別,div是可以識別的,呵呵:)

希望幫到你:)

HTML中的meta標籤是什麼作用

幾乎所有的網頁裡,我們可以看到類似下面這段的也許你認為這些 可有可無。其實如果你能夠用好meta標籤,會給你帶來意想不到的效果,例如加入關鍵字會自動被大型搜尋 自動蒐集 可以設定頁面格式及重新整理等等。meta標籤的組成 請問html 中的 標籤是幹什麼用的?meta標籤是html語言head區的一...

html的thead標籤怎麼顯示

id是systemplatfrom的table,這張表好像顯示不出來。首先我沒看到這張表,只看到systemplatfrompage,這個 只是顯示一個表頭,顯示出來的只是 操作,頁面編號.是否執行這幾個字而已 至於你說的沒顯示出來,會不會是因為你沒有資料,這個看起來像是一個動態頁面,你得看對他操作...

li標籤中怎麼設定文字居中對齊

預設為左對齊 常用的對齊方式如下 內容1 居中對齊 內容2 左對齊 內容3 右對齊 li這樣 居中的文字 這樣寫就可以了 display block text align center 不用謝 css樣式怎樣設定 標籤中li內容居中左對齊?20 額。你到底是想居中還是左對齊?居中就直接用text a...