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

2021-06-26 08:11:19 字數 4697 閱讀 2287

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文字標籤有哪些,常用的HTML文字標籤有哪些

定義註釋 定義文件型別 定義錨 定義縮寫 定義只取首字母的縮寫 定義文件作者或擁有者的聯絡資訊。擴充套件資料 html特點如下 1 簡易性 超級文字標記語言版本升級採用超集方式,從而更加靈活方便。2 可擴充套件性 超級文字標記語言的廣泛應用帶來了加強功能,增加識別符號等要求,超級文字標記語言採取子類...

在html中怎麼讓文字垂直居中,如何使文字在div中水平和垂直居中的css程式碼,div水平垂直居中div

使文字在div中水平 和垂直居中的的css樣式為 text align center 水平居中 line height 20px 行距設為與div高度一致 示例如專 下 html元素屬 水平垂直居中 css樣式div height 50px lien height 50px 如何使文字在div中水平...

css中怎麼設定文字垂直居中,在html中怎麼讓文字垂直居中

單行很好解決 height18px line height 18px 高和行高值相同即可 但對於多行,你可以看下這裡,純css實現瀏覽器相容的 上下左右都居中的內容 css中有一個屬性 line height如果你的文字p,span,div,title等那麼line height的值到底等於多少居中...