Programming/MS-SQL

MS-SQL: html 태그 제거하기 / html tag remove

고고마코드 2022. 3. 8. 15:57
반응형

html 태그 제거 함수

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[cleanHTML](@strHTML VARCHAR(8000)) 

RETURNS VARCHAR(8000)
AS 
BEGIN
    DECLARE @strTEXT VARCHAR(8000)
    DECLARE @char VARCHAR(2)
    DECLARE @len INT
    DECLARE @i INT

    SET @strTEXT = ''
    SET @char = ''

    SET @len = LEN(@strHTML)
    SET @i = 1

    WHILE @i <= @len
        BEGIN

            SET @char = SUBSTRING(@strHTML, @i, 1) 

            IF @char = '<' 
                BEGIN

                    WHILE @i <= @len and @char <> '>'
                        BEGIN

                            SET @i = @i + 1
                            SET @char = SUBSTRING(@strHTML, @i, 1) 

                        END
                END
            ELSE
                BEGIN

                    SET @strTEXT = @strTEXT + REPLACE(REPLACE(REPLACE(@char, CHAR(9), ''), CHAR(10), ''), CHAR(13), '')

                END


            SET @i = @i + 1

        END

    SET @strTEXT = LTRIM(LTRIM(@strTEXT))
    SET @strTEXT = REPLACE(@strTEXT, ' ', ' ')
    SET @strTEXT = REPLACE(@strTEXT, '>', '>')
    SET @strTEXT = REPLACE(@strTEXT, '<', '<')
    SET @strTEXT = REPLACE(@strTEXT, '"', '''')

    RETURN (@strTEXT)
END

활용 예시

SELECT dbo.cleanHTML('<h2>제목</h2><div><p>내용1</p><p>내용2</p></div>') AS 'cleanHTML'

제목내용1내용2

참고자료

https://ggmouse.tistory.com/114

반응형

'Programming > MS-SQL' 카테고리의 다른 글

MS-SQL: 정규식 사용하기 / GetRegExReplace  (0) 2022.03.08