去除字符串中的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& ...
随机推荐
- PHP输出Excel两种方法
2016年3月23日 16:43:51 星期三 第一种: 输出html+css格式, 打开后用Excel软件的"另存为"功能保存为正规的表格格式 public function e ...
- Java for LeetCode 220 Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【编程题目】输出 1 到最大的 N 位数
65.输出 1 到最大的 N 位数(运算)题目:输入数字 n,按顺序输出从 1 最大的 n 位 10 进制数.比如输入 3,则输出 1.2.3 一直到最大的 3 位数即 999. 思路:肯定要考虑数字 ...
- 【vs2010调试】当前不会命中断点 源代码与原始版本不同
解决方案:全选CPP文件内容,选择 “编辑”-“高级”-“设置选定内容的格式”,保存,重新编译.
- 【python】为什么修改全局的dict变量不用global关键字
转自:http://my.oschina.net/leejun2005/blog/145911?fromerr=qnPCgI19#OSC_h4_8 为什么修改字典d的值不用global关键字先声明呢? ...
- [Android Pro] AES加密
reference to :http://blog.csdn.net/wfung_kwok/article/details/7766427 package com.secufity.aes; impo ...
- September 23rd 2016 Week 39th Friday
Even a small star shines in the darkness. 星星再小,也会发光. In the darkness, even a small star can shine. N ...
- function与感叹号
原文链接:https://swordair.com/function-and-exclamation-mark/ 最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)
题目链接:http://codeforces.com/contest/722/problem/D 1 #include <bits/stdc++.h> #include <iostr ...