次のような 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' )
: SQL関数は<BR>
and<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