-- This table contains the tags to be replaced. The % in 
-- will take care of any extra information in the tag that you needn't worry
-- about as a whole. In any case, this table contains all the tags that needs
-- to be search & replaced.
create table #html ( tag varchar(30) )
insert #html values ( '' )
insert #html values ( '' )
insert #html values ( '' )
insert #html values ( '' )
insert #html values ( '' )
insert #html values ( '' )
insert #html values ( '' )
insert #html values ( '' )
go

-- A simple table with the HTML strings
create table #t ( id tinyint identity , string varchar(255) ) 
insert #t values (
'Some Name


Some HTML text after the body'
)
insert #t values (
'Another Name

Another HTML text after the body'
)
go

-- This is the code to strip the tags out.
-- It finds the starting location of each tag in the HTML string ,
-- finds the length of the tag with the extra properties if any. This is
-- done by locating the end of the tag namely '>'. The same is done
-- in a loop till all tags are replaced.
begin tran
while exists(select * from #t join #html on patindex('%' + tag + '%' , string ) > 0 )
        update #t
        set string = stuff( string , patindex('%' + tag + '%' , string ) ,
                                charindex( '>' , string , patindex('%' + tag + '%' , string ) )
                                - patindex('%' + tag + '%' , string ) + 1 , '' )
        from #t join #html
        on patindex('%' + tag + '%' , string ) > 0

select * from #t
rollback
This page was last updated on May 01, 2006 04:28 PM.