我们通常用富文本框编辑内容存在数据库中,在做列表显示数据的时候不想显示原来的样式,用自定义函数格式化html标签即可。
自定义函数
alter function [dbo].[dropHtmlTag](@cont varchar(max)) | |
returns varchar(max) | |
as | |
begin | |
--去掉style标签 | |
declare @stylecount int; | |
set @stylecount = charindex('<style>',@cont); | |
while @stylecount>0 | |
begin | |
select @cont=replace(@cont, SUBSTRING(@cont,CHARINDEX('<style>',@cont),CHARINDEX('</style>',@cont)-CHARINDEX('<style>',@cont)+8),''); | |
set @stylecount = charindex('<style>',@cont); | |
end | |
--去掉script标签 | |
declare @scriptcount int; | |
set @scriptcount = charindex('<script',@cont); | |
while @scriptcount>0 | |
begin | |
select @cont=replace(@cont, SUBSTRING(@cont,CHARINDEX('<script',@cont),CHARINDEX('</script>',@cont)-CHARINDEX('<script',@cont)+9),''); | |
set @scriptcount = charindex('<script',@cont); | |
--去掉内容标签 | |
declare @tem varchar(max),@i int,@j int; | |
set @tem = ISNULL(@cont,''); | |
set @i = charindex('<',@tem,0); | |
set @j = charindex('>',@tem,1); | |
while @i < @j | |
begin | |
select @tem = replace(@tem,SUBSTRING(@tem,@i,@j-@i+1),'') | |
set @i = charindex('<',@tem,0); | |
set @j = charindex('>',@tem,1); | |
end | |
return @tem | |
end |