sql去除html标签

 

sql去除html标签

分类: sql
 

--1、创建函数

[sql] view plaincopy
 
  1. create function [dbo].[clearhtml] (@maco varchar(8000))   
  2.   
  3. returns varchar(8000) as begin   
  4.   
  5.     declare @i int   
  6.   
  7.     while 1 = 1    
  8.   
  9.     begin   
  10.   
  11.        set @i=len(@maco)   
  12.   
  13.        set @maco=replace(@maco, substring(@maco,charindex('<',@maco),   
  14.   
  15.        charindex('>',@maco)-charindex('<',@maco)+1),space(0))   
  16.   
  17.        if @i=len( @maco )    
  18.   
  19.        break    
  20.   
  21.     end   
  22.   
  23.     
  24.   
  25.     set @maco=replace(@maco,' ','')   
  26.   
  27.     set @maco=replace(@maco,' ','')   
  28.   
  29.     set @maco=ltrim(rtrim(@maco))   
  30.   
  31.     set @maco=replace(@maco,char(9),'')   
  32.   
  33.     set @maco=replace(@maco,char(10),'')   
  34.   
  35.     set @maco=replace(@maco,char(13),'')   
  36.   
  37.     
  38.   
  39.     return (@maco)   
  40.   
  41. end   


--2、测试示例 

[sql] view plaincopy
 
  1. declare @mark varchar(8000)   
  2.   
  3. set @mark='<body><div id=u><a href=http://passport.baidu.com/?login&tpl=mn>登录</a></div><center><img src=/upload/2011/3/2210597443.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 onclick=s(this) href=http://news.baidu.com>新 闻</a><b>网 页</b><a onclick=s(this) href=http://tieba.baidu.com>贴 吧</a><a onclick=s(this) href=http://zhidao.baidu.com>知 道</a><a onclick=s(this) href=http://mp3.baidu.com>MP3</a><a onclick=s(this) href=http://image.baidu.com>图 片</a><a onclick=s(this) href=http://video.baidu.com>视 频</a></div></td></tr></table>   
  4.   
  5. <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>   
  6.   
  7. </body>'   
  8.   
  9. select dbo.clearhtml (@mark)   

--3、运行结果 

/* 
new 
--------------------------------------- 
登录新闻网页贴吧知道MP3图片视频帮助高级 

*/


/* 
但是上面的函数还存在问题,如果内容中有“《》”或是“<<>>”这样的标记,则不能达到我们的要求。 
*/ 

 

--加强版 

[sql] view plaincopy
 
  1. create function [dbo].[clearhtml_V2] (@maco varchar(8000))   
  2.   
  3. returns varchar(8000)    
  4.   
  5. as    
  6.   
  7. begin   
  8.   
  9.     declare @randchar_one nvarchar(200)   
  10.   
  11.     declare @randchar_two nvarchar(200)   
  12.   
  13.        if(charindex('<<',@maco)>0)   
  14.   
  15.               begin   
  16.   
  17.                      set @randchar_one='D4678B36-B958-4274-B81E-BBA636CFB427';   
  18.   
  19.                      set @randchar_two='49E374CC-9E1A-4850-897C-27074DE32E7F';   
  20.   
  21.                      set @maco=replace(@maco,'<<',@randchar_one)   
  22.   
  23.                      set @maco=replace(@maco,'>>',@randchar_two)   
  24.   
  25.               end   
  26.   
  27.     declare @i int   
  28.   
  29.     while 1 = 1    
  30.   
  31.     begin   
  32.   
  33.        set @i=len(@maco)   
  34.   
  35.        set @maco=replace(@maco, substring(@maco,charindex('<',@maco),   
  36.   
  37.        charindex('>',@maco)-charindex('<',@maco)+1),space(0))   
  38.   
  39.        if @i=len( @maco )    
  40.   
  41.        break    
  42.   
  43.     end   
  44.   
  45.     
  46.   
  47.     set @maco=replace(@maco,' ','')   
  48.   
  49.     set @maco=replace(@maco,' ','')   
  50.   
  51.     set @maco=ltrim(rtrim(@maco))   
  52.   
  53.     set @maco=replace(@maco,char(9),'')   
  54.   
  55.     set @maco=replace(@maco,char(10),'')   
  56.   
  57.     set @maco=replace(@maco,char(13),'')   
  58.   
  59.     if(charindex(@randchar_one,@maco)>0)   
  60.   
  61.     begin   
  62.   
  63.        set @maco=replace(@maco,'D4678B36-B958-4274-B81E-BBA636CFB427','<<')   
  64.   
  65.        set @maco=replace(@maco,'49E374CC-9E1A-4850-897C-27074DE32E7F','>>')   
  66.   
  67.     end   
  68.   
  69.     return (@maco)   
  70.   
  71. end   
  72.   
  73.     
  74.   
  75. select dbo.clearhtml_V2('<p>test</p><<西游记>><a href="www.baidu.com" />')   

--运行结果: 

/* 
test<<西游记>> 
posted @ 2015-10-30 10:11  happyu0223  阅读(1031)  评论(0编辑  收藏  举报