日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

去除字符串中的html标记及标记中的内容

發布時間:2025/4/5 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 去除字符串中的html标记及标记中的内容 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

去除字符串中的html標記及標記中的內容

--1、創建函數

create?function?[dbo].[clearhtml]?(@maco?varchar(8000))

returns?varchar(8000)?as?begin

????declare?@i?int

????while?1?=?1

????begin

???????set?@i=len(@maco)

???????set?@maco=replace(@maco,?substring(@maco,charindex('<',@maco),

???????charindex('>',@maco)-charindex('<',@maco)+1),space(0))

???????if?@i=len(?@maco?)

???????break

????end

?

????set?@maco=replace(@maco,' ','')

????set?@maco=replace(@maco,'&nbsp;','')

????set?@maco=ltrim(rtrim(@maco))

????set?@maco=replace(@maco,char(9),'')

????set?@maco=replace(@maco,char(10),'')

????set?@maco=replace(@maco,char(13),'')

?

????return?(@maco)

end

?

--2、測試示例

declare?@mark?varchar(8000)

set?@mark='<body><div id=u><a href=http://passport.baidu.com/?login&tpl=mn>登錄</a></div><center><img src=http://www.baidu.com/img/baidu_logo.gif width=270 height=129 usemap="#mp" id=lg><br><br><br><br><table cellpadding=0 cellspacing=0 id=l><tr><td><div id=m><a οnclick=s(this) href=http://news.baidu.com>新&nbsp;聞</a><b>網&nbsp;頁</b><a οnclick=s(this) href=http://tieba.baidu.com>貼&nbsp;吧</a><a οnclick=s(this) href=http://zhidao.baidu.com>知&nbsp;道</a><a οnclick=s(this) href=http://mp3.baidu.com>MP3</a><a οnclick=s(this) href=http://image.baidu.com>圖&nbsp;片</a><a οnclick=s(this) href=http://video.baidu.com>視&nbsp;頻</a></div></td></tr></table>

<table cellpadding=0 cellspacing=0 style="margin-left:15px"><tr valign=top><td style="height:62px;padding-left:92px" nowrap><div style="position:relative"><form name=f action=/s><input type=text name=wd id=kw size=42 maxlength=100> <input type=submit value=百度一下id=sb><div id=sug onselectstart="return false"></div><span id=hp><a href=/search/jiqiao.html>幫助</a><br><a href=/gaoji/advanced.html>高級</a></span></form></div></td></tr></table>

</body>'

select?dbo.clearhtml?(@mark)

?

--3、運行結果

/*

new

---------------------------------------

登錄新聞網頁貼吧知道MP3圖片視頻幫助高級

*/

/*

但是上面的函數還存在問題,如果內容中有“《》”或是“<<>>”這樣的標記,則不能達到我們的要求。

*/

?

--加強版

create?function?[dbo].[clearhtml_V2]?(@maco?varchar(8000))

returns?varchar(8000)

as

begin

????declare?@randchar_one?nvarchar(200)

????declare?@randchar_two?nvarchar(200)

???????if(charindex('<<',@maco)>0)

??????????????begin

?????????????????????set?@randchar_one='D4678B36-B958-4274-B81E-BBA636CFB427';

?????????????????????set?@randchar_two='49E374CC-9E1A-4850-897C-27074DE32E7F';

?????????????????????set?@maco=replace(@maco,'<<',@randchar_one)

?????????????????????set?@maco=replace(@maco,'>>',@randchar_two)

??????????????end

????declare?@i?int

????while?1?=?1

????begin

???????set?@i=len(@maco)

???????set?@maco=replace(@maco,?substring(@maco,charindex('<',@maco),

???????charindex('>',@maco)-charindex('<',@maco)+1),space(0))

???????if?@i=len(?@maco?)

???????break

????end

?

????set?@maco=replace(@maco,' ','')

????set?@maco=replace(@maco,'&nbsp;','')

????set?@maco=ltrim(rtrim(@maco))

????set?@maco=replace(@maco,char(9),'')

????set?@maco=replace(@maco,char(10),'')

????set?@maco=replace(@maco,char(13),'')

????if(charindex(@randchar_one,@maco)>0)

????begin

???????set?@maco=replace(@maco,'D4678B36-B958-4274-B81E-BBA636CFB427','<<')

???????set?@maco=replace(@maco,'49E374CC-9E1A-4850-897C-27074DE32E7F','>>')

????end

????return?(@maco)

end

?

select?dbo.clearhtml_V2('<p>aaaa</p><<本草綱目>><a href="www.baidu.com" />')

--運行結果:

/*

aaaa<<本草綱目>>

*/

總結

以上是生活随笔為你收集整理的去除字符串中的html标记及标记中的内容的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。