2015-04-11

TSQL - Function to remove HTML tags

ALTER FUNCTION dbo.getNoHTML
(
       @HTMLText VARCHAR(MAX)
)
RETURNS VARCHAR(MAX) AS
BEGIN
    DECLARE @Start INT
    DECLARE @End INT
    DECLARE @Length INT
    SET @Start = CHARINDEX('<',@HTMLText)
    SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
    SET @Length = (@End - @Start) + 1
      
       --removing ¿
       set @HTMLText = replace(@HTMLText, '¿','')

       --removing HTML <>
    WHILE @Start > 0 AND @End > 0 AND @Length > 0
    BEGIN
       if CHARINDEX('<',@HTMLText,CHARINDEX('<',@HTMLText)+1) between CHARINDEX('<',@HTMLText) and CHARINDEX('>',@HTMLText)
              begin
                     SET @HTMLText = STUFF(
                                                       @HTMLText
                                                       ,CHARINDEX('<',@HTMLText,CHARINDEX('<',@HTMLText)+1)
                                                       ,(CHARINDEX('>',@HTMLText)+1) - CHARINDEX('<',@HTMLText,CHARINDEX('<',@HTMLText)+1)
                                                       ,' ')
                     SET @Start = CHARINDEX('<',@HTMLText)
                     SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
                     SET @Length = (@End - @Start) + 1
              end
       else
              begin
              -- this is the good one
                     SET @HTMLText = STUFF(@HTMLText,@Start,@Length,' ')
                     SET @Start = CHARINDEX('<',@HTMLText)
                     SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
                     SET @Length = (@End - @Start) + 1
              end
    END



    RETURN LTRIM(RTRIM(@HTMLText))
END


No comments:

Post a Comment