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

時間 2021-06-26 08:11:40

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中怎麼讓兩個《a》標籤不換行

水果風 標籤預設是不換行的,如果因為某些樣式共同存在,可能會出現樓主說的情況,這時候可以這樣,讓不換行比如 11 div a 希望能幫助到樓主,往採納! 花果山小猴 行內元素在編輯器中換行的話會預設出半個字元的空格 html中怎麼讓兩個標籤不換行? 橋凱青景鑠 html中有兩類元素 也就是標籤 bl...

HTML中怎麼讓標籤不執行讓它在網頁中以文字的方式顯示

在網頁中以文字顯示html標籤,可以使用html標籤內容,這樣,在網頁中就會顯示html標籤,如圖 澤希 把 換成 lt 把 後面的空格去掉 把 換成 gt 把 後面的空格去掉 比如 換成 lt br gt 123換成 lt div gt 123 lt div gt 就可以在網頁上顯示出來了。注意 ...

HTML標籤中同td裡如何設定元件的橫向間距

有兩種方法 一 用css實現如下 button 二 用 nbsp 實現如下 提交?nbsp nbsp nbsp nbsp 重置注 把 去掉 css裡怎麼設定兩個td之間的橫向距離 請問 td 的橫向距離是要實現怎樣的效果?其實可以通過 td 設定 padding,而 td 不設定邊框,在 td 裡面...