sql去除html標籤

 

sql去除html標籤

分類: sql
 

--一、建立函數html

[sql]  view plain copy
 
  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   


--二、測試示例 sql

[sql]  view plain copy
 
  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)   

--三、運行結果 

/* 
new 
--------------------------------------- 
登陸新聞網頁貼吧知道MP3圖片視頻幫助高級 

*/數據庫


/* 
可是上面的函數還存在問題,若是內容中有「《》」或是「<<>>」這樣的標記,則不能達到咱們的要求。 
*/ ide

 

--增強版 函數

[sql]  view plain copy
 
  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<<西遊記>> 
相關文章
相關標籤/搜索