DevPinoy.org
A Filipino Developers Community
   
A custom T-SQL function for removing embedded HTML tags

This is an interesting piece of sql, the function accepts a string and removes HTML elements that is inside the string. This is genereally useful when you have data that has embedded HTML content on it. The function was written by Robert Davis.

/******
Object: User Defined Function dbo.fnStripTags
Script Author: Robert Davis, robertd@realtechllc.com
Purpose: Strip HTML tags out of text. Anything enclosed in '<' and '>' will be removed.

sample usage:
Declare @MyText varchar(30)
Set @MyText = '<b>My <i>sample</i> Text</b>'

Select dbo.fnStripTags(@MyText)

this returns: My sample Text

******/


Create Function dbo.fnStripTags
    (@Dirty varchar(4000))
    Returns varchar(4000)
As

Begin
    Declare @Start int,
        @End int,
        @Length int

    While CharIndex('<', @Dirty) > 0 And CharIndex('>', @Dirty, CharIndex('<', @Dirty)) > 0
     Begin
        Select @Start = CharIndex('<', @Dirty),
         @End = CharIndex('>', @Dirty, CharIndex('<', @Dirty))
        Select @Length = (@End - @Start) + 1
        If @Length > 0
         Begin
            Select @Dirty = Stuff(@Dirty, @Start, @Length, '')
         End
     End

    return @Dirty
End


Posted 06-23-2005 3:36 PM by keithrull
Filed under: ,

Comments

Michel DEGREMONT wrote re: A custom T-SQL function for removing embedded HTML tags
on 09-30-2006 11:36 AM

Hi Keith Rull,

If I have properties in my tag html, this function doesn't work.

Declare @MyText varchar(30)

Set @MyText = '<b>My <font color=red>sample</font> Text</b>'

Select dbo.fnStripTags(@MyText)

Have you got an idea to resolve this issue ?

Removing embedded HTML tags with SQL « SourceFirst wrote Removing embedded HTML tags with SQL &laquo; SourceFirst
on 06-13-2007 3:42 AM

Pingback from  Removing embedded HTML tags with SQL &laquo; SourceFirst


Copyright DevPinoy 2005-2008