您可以使用 SQL 函数,例如:
ALTER FUNCTION [dbo].[StripOutHTML]
(
@HTMLText VARCHAR(max),
@stripDisallowedOnly BIT
)
returns VARCHAR(max)
AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
-- Replace the HTML entity & with the '&' character (this needs to be done first, as
-- '&' might be double encoded as '&')
SET @Start = Charindex('&', @HTMLText)
SET @End = @Start + 4
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '&')
SET @Start = Charindex('&', @HTMLText)
SET @End = @Start + 4
SET @Length = ( @End - @Start ) + 1
END
-- Replace the HTML entity < with the '<' character
SET @Start = Charindex('<', @HTMLText)
SET @End = @Start + 3
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '<')
SET @Start = Charindex('<', @HTMLText)
SET @End = @Start + 3
SET @Length = ( @End - @Start ) + 1
END
-- Replace the HTML entity > with the '>' character
SET @Start = Charindex('>', @HTMLText)
SET @End = @Start + 3
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '>')
SET @Start = Charindex('>', @HTMLText)
SET @End = @Start + 3
SET @Length = ( @End - @Start ) + 1
END
-- Replace the HTML entity & with the '&' character
SET @Start = Charindex('&amp;', @HTMLText)
SET @End = @Start + 4
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '&')
SET @Start = Charindex('&amp;', @HTMLText)
SET @End = @Start + 4
SET @Length = ( @End - @Start ) + 1
END
-- Replace the HTML entity with the ' ' character
SET @Start = Charindex(' ', @HTMLText)
SET @End = @Start + 5
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, ' ')
SET @Start = Charindex(' ', @HTMLText)
SET @End = @Start + 5
SET @Length = ( @End - @Start ) + 1
END
-- Replace any <P>, </P>tags with a <BR>, so they will be replaced with a new line in next step
SET @HTMLText = REPLACE(@HTMLText, '<P>', '<br>')
SET @HTMLText = REPLACE(@HTMLText, '</P>', '<br>')
-- Replace any <BR> tags with a newline
SET @Start = Charindex('<br>', @HTMLText)
SET @End = @Start + 3
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length,
Char(13) + Char(10))
SET @Start = Charindex('<br>', @HTMLText)
SET @End = @Start + 3
SET @Length = ( @End - @Start ) + 1
END
-- Replace any tags with a newline
SET @Start = Charindex('<br/>', @HTMLText)
SET @End = @Start + 4
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length,
'CHAR(13) + CHAR(10)')
SET @Start = Charindex('<br/>', @HTMLText)
SET @End = @Start + 4
SET @Length = ( @End - @Start ) + 1
END
-- Replace any tags with a newline
SET @Start = Charindex('<br />', @HTMLText)
SET @End = @Start + 5
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length,
'CHAR(13) + CHAR(10)')
SET @Start = Charindex('<br />', @HTMLText)
SET @End = @Start + 5
SET @Length = ( @End - @Start ) + 1
END
-- Remove anything between tags
SET @Start = Charindex('<', @HTMLText)
SET @End = Charindex('>', @HTMLText, Charindex('<', @HTMLText))
SET @Length = ( @End - @Start ) + 1
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
IF @stripDisallowedOnly = 1
BEGIN
IF ( Upper(Substring(@HTMLText, @Start, 2)) <> '<B' )
AND ( Upper(Substring(@HTMLText, @Start, 3)) <> '</B' )
AND ( Upper(Substring(@HTMLText, @Start, 2)) <> '<U' )
AND ( Upper(Substring(@HTMLText, @Start, 3)) <> '</U' )
AND ( Upper(Substring(@HTMLText, @Start, 2)) <> '<I' )
AND ( Upper(Substring(@HTMLText, @Start, 3)) <> '</I' )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '')
END
ELSE
BEGIN
SET @Length = 0
END
END
ELSE
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '')
END
SET @Start = Charindex('<', @HTMLText, @End - @Length)
SET @End = Charindex('>', @HTMLText, Charindex('<', @HTMLText,
@Start)
)
SET @Length = ( @End - @Start ) + 1
END
-- Remove any leading space/carriage return
DECLARE @trimchars VARCHAR(10)
SET @trimchars = CHAR(9)+CHAR(10)+CHAR(13)+CHAR(32)
IF @HTMLText LIKE '[' + @trimchars + ']%' SET @HTMLText = SUBSTRING(@HTMLText, PATINDEX('%[^' + @trimchars + ']%', @HTMLText), LEN(@HTMLText))
RETURN Ltrim(Rtrim(@HTMLText))
END
您需要更改以下行以保留em
类似于以下的标签Upper(Substring(@HTMLText, @Start, 2)) <> '<B' )
:在正确的方向。<BR>
<P>
WHILE ( @Start > 0
AND @End > 0
AND @Length > 0 )
BEGIN
IF @stripDisallowedOnly = 1
BEGIN
IF ( Upper(Substring(@HTMLText, @Start, 2)) <> '<B' )
AND ( Upper(Substring(@HTMLText, @Start, 3)) <> '</B' )
AND ( Upper(Substring(@HTMLText, @Start, 2)) <> '<U' )
AND ( Upper(Substring(@HTMLText, @Start, 3)) <> '</U' )
AND ( Upper(Substring(@HTMLText, @Start, 2)) <> '<I' )
AND ( Upper(Substring(@HTMLText, @Start, 3)) <> '</I' )
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '')
END
ELSE
BEGIN
SET @Length = 0
END
END
ELSE
BEGIN
SET @HTMLText = Stuff(@HTMLText, @Start, @Length, '')
END
SET @Start = Charindex('<', @HTMLText, @End - @Length)
SET @End = Charindex('>', @HTMLText, Charindex('<', @HTMLText,
@Start)
)
SET @Length = ( @End - @Start ) + 1
END