【叶子函数分享一】去除字符串中的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<<本草纲目>>

*/

 

特别说明:

如果数据量比较大,尽量避免使用自定义函数,以免严重影响性能。

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值