我想从 MSSQLSERVER 中的[Contnent]
中删除所有内联样式从样式="*"中清除所有内容
例如: <div style="color:red;font-size:12px;"> test <i style="color:blue;" > 2 </i> </div>
自: <div> test <i> 2 </i> </div>
在您的情况下,定义函数以删除所有标签很有用:
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
我找到了这个函数,但它删除了 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