去除字符串中的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,' ','')
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 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>
<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,' ','')
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标记及标记中的内容的更多相关文章
- PHP中去除字符串中的换行的方法
在PHP中,有时候我们需要对字符串的换行进行过滤,比如天涯PHP博客中文章页面的description信息,我是直接截取的文章内容,并过滤掉html符号,最终还要过滤掉其中的换行.下面整理一下常见的去 ...
- 换行符javajava去除字符串中的空格、回车、换行符、制表符
在改章节中,我们主要介绍换行符java的内容,自我感觉有个不错的建议和大家分享下 每日一道理 只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获.只有追求,才会 ...
- javascript 去除字符串中重复字符
/** * 去除字符串中重复的字符,以下提供2种方法, * removeRepeat()为自己所想: * removeRepeat2()参考网上思路补充的 * removeRepeat3()敬请期待· ...
- js去除字符串中所有html标签及 符号
近日在做项目的时候,经常会在页面上处理一些数据.结果发现自己js掌握的并不是很好.那就在这里记录js的点点滴滴吧. 1. 去除字符串中的 html 标签 function delHtmlTag(str ...
- python_如何去除字符串中不想要的字符?
问题: 过滤用户输入中前后多余的空白字符 ' ++++abc123--- ' 过滤某windows下编辑文本中的'\r': 'hello world \r\n' 去掉文本中unicode组 ...
- 正则去除字符串中的html标签,但不去除<br>标签
一.去除html标签 filterHTMLTag(msg) { var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag msg ...
- Linux shell去除字符串中所有空格
Linux shell去除字符串中所有空格 echo $VAR | sed 's/ //g'
- js去除字符串中的标签
var str="<p>js去除字符串中的标签</p>"; var result=str.replace(/<.*?>/ig,"&qu ...
- dom4j解析xml报"文档中根元素后面的标记格式必须正确"
今天,在写个批量启动报盘机的自动化应用,为了简化起见,将配置信息存储在xml中,格式如下: <?xml version="1.0" encoding="UTF-8& ...
随机推荐
- ACM/ICPC 之 网络流入门-Ford Fulkerson与SAP算法(POJ1149-POJ1273)
第一题:按顾客访问猪圈的顺序依次构图(顾客为结点),汇点->第一个顾客->第二个顾客->...->汇点 //第一道网络流 //Ford-Fulkerson //Time:47M ...
- ACM/ICPC 之 拓扑排序-反向(POJ3687)
难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重 ...
- Python 输入输出
语法注释 输入输出 #语法缩进,4个空格 #注释 #冒号:结尾,缩进的预计视为代码块 #大小写敏感 #输出 print 300 print 'hello','world' #输入 a=raw_inpu ...
- [转]AndroidStudio导出jar包
原文链接:http://blog.csdn.net/hjq842382134/article/details/38538097# 1. 不像在Eclipse,可以直接导出jar包.AndroidStu ...
- 如何让两个 并列的div高度相等
哪个div Height值大,就将其值赋给Height值小的div,从而使2个div高度始终保持一致. function $(id){ return document.getElementById(i ...
- 关于js事件委托
由于事件处理程序可以为现代 Web 应用程序提供交互能力,因此许多开发人员会不分青红皂白地 向页面中添加大量的处理程序. 在创建 GUI 的语言(如 C#)中,为 GUI 中的每个按钮添加一个 onc ...
- IOS searchBar去掉背景
修改UISearchBar的背景颜色 UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的 ...
- JS图表插件(柱形图、饼状图、折线图)
http://www.open-open.com/lib/view/open1406378625726.html
- 编译QtAV工程库
去https://github.com/wang-bin/QtAV下载源代码 去https://sourceforge.net/projects/qtav/files/depends/QtAV-dep ...
- UIColor+Hex
#import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *)colorWithHex:(long)hexColor;+ (U ...