从MSSQL中的文本中删除所有内联样式

时间:2018-02-28 08:24:02

标签: html sql sql-server regex

我想从 MSSQLSERVER 中的[Contnent]删除所有内联样式 从style =“*”

清除一切

for ex: <div style="color:red;font-size:12px;"> test <i style="color:blue;" > 2 </i> </div>

TO: <div> test <i> 2 </i> </div>

2 个答案:

答案 0 :(得分:1)

在您的情况下,定义删除所有标记的功能非常有用:

create function RemoveStyles(@html varchar(1000)) returns varchar(1000) as
begin

declare @tagIndex int
set @tagIndex = CHARINDEX(' style=', @html)

while @tagIndex > 0
begin
    set @html = SUBSTRING(@html, 1, @tagIndex - 1) + SUBSTRING(@html, CHARINDEX('"', @html, @tagIndex + 8) + 1, LEN(@html))
    set @tagIndex = CHARINDEX(' style=', @html)
end
return @html
end

然后您的查询将变得非常简单(我在master数据库中创建了我的函数):

select master.dbo.RemoveStyles([Content]) from MY_TABLE

答案 1 :(得分:0)

我找到了这个功能,但它删除了STYLE标签

  create FUNCTION [dbo].CleanHTML(@HTMLText varchar(MAX))
RETURNS varchar(MAX)
AS
BEGIN
DECLARE @Start  int
DECLARE @End    int
DECLARE @Length int


SET @Start = CHARINDEX('<STYLE', @HTMLText)
SET @End = CHARINDEX('</STYLE>', @HTMLText, CHARINDEX('<', @HTMLText)) + 7
SET @Length = (@End - @Start) + 1

WHILE (@Start > 0 AND @End > 0 AND @Length > 0) BEGIN
SET @HTMLText = STUFF(@HTMLText, @Start, @Length, '')
SET @Start = CHARINDEX('<STYLE', @HTMLText)
SET @End = CHARINDEX('</STYLE>', @HTMLText, CHARINDEX('</STYLE>', @HTMLText)) + 7
SET @Length = (@End - @Start) + 1
END


RETURN LTRIM(RTRIM(@HTMLText))

END