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

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

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標籤logo怎麼新增上去的

先把logo轉換成字尾名是ico的 然後在網頁頭部,也就是中間放上這一段 注意 路徑一定要寫對,絕對路徑或者相對路徑都沒關係,然後就ok了 如何在網頁的標題欄上新增上小logo?直接用ps開啟,裡面就有儲存為ico格式的型別即可 link rel icon href 的位置.ico html標籤lo...

html怎麼讓span標籤的輪廓變成圓的

這個並不複雜,可以使用css3中是一個屬性解決。border radius 屬性是一個簡寫屬性,用於設定四個 border radius 屬性。border radius 2em 等價於同時設定了四個角 border topradius 2em border right radius 2em bor...

如何讓HTML中的文字與圖示居中對齊

方法一 設定img的vertical align屬性為top2方法二 設定img的margin bottom屬性為 xend注意事項 方法二如果 比較大可以將值設定大一些 css html 如何讓div裡邊的 和文字同時上下居中?img標籤是行內元素,所以設定文字和 垂直居中只需要把div的line...