Tel. +44(0)208 4716864
info@softstuff-consulting.com
Follow us on Twitter  http://www.twitter.com/softstuffc
Softstuff Home Knowledgebase Home Contact Us Blog
GoldMine CRM SQL Server QuoteWerks Microsoft Excel Microsoft Word C# Windows XP Microsoft Dynamics CRM HEAT Sage CRM
Function to strip HTML from a string

Below is a function that can be used to strip HTML from a string. It works by looping through the string until it meets an opening HTML tag, and ignores everyting until it meets a closing tag.

Finally it replaces the HTML characters before returning the result.

 
Create function SS_StripHTML(@html varchar(max)) returns varchar(max)
as
Begin
declare @p int
declare @nonspace bit
declare @istag bit
declare @chr varchar(1)
declare @result varchar(max)
set @p=1
set @istag=0
set @result=''
set @nonspace=0


--Tidy up the Raw HTML before we start, not interested in carriage returns,
--but we want to keep the <br> for later.
set @html=replace(@html,char(13)+char(10),'')
set @html=replace(@html,'<br>','{&&BR&&}')

loop around the text
while @p<len(@html)
begin
set @chr=substring(@html,@p,1)

--is it the start of a HTML tag, if so do nothing until we meet a closing tag
if @chr='<' set @istag=1
--is it a closing tag
if @chr='>'
begin
set @istag=0
set @nonspace=0
end
else
begin
--is it space, because HTML can contain lots of blank space between tags we
--aren’t interested in, especially if its hand written HTML code.
if ascii(@chr)>32 set @nonspace=1
--if its not space set a flag and from then on we can collect characters for
--the result until we meet another HTML tag.
if @istag=0 and @nonspace=1 set @result=@result+@chr
end
set @p=@p+1

end

--clean up the code and replace some of the HTML codes
--string concats are to prevent errors displaying in this page but codes
--can go into one string.
set @result=replace(@result,'&nbs'+'p;',' ');
set @result=replace(@result,'&am'+'p;','&');
set @result=replace(@result,'&l'+'t;','<');
set @result=replace(@result,'&g'+'t;','>');
set @result=replace(@result,'&quo'+'t;','"');
--add our line feeds back in
set @result=replace(@result,'{&&BR&&}',char(13)+char(10));

return @result

end



To call the function use
Select dbo.SS_StripHTML('<b>This is an example</br>')


06 August 2009  IB      SQL Server


Back To List

Did this article answer your question

 Yes

 No

 



 

Search our knowledgebase

Look In   For  
Enter a word or phrase to search for. eg. shrink

Enter your Email Address to receive the latest knowledgebase items delivered direct to your inbox.

Subscribe to the knowledgebase in your favourite RSS reader:   Plain RSS 2.0 Feed   Add to Google  Add to My Yahoo!

Note:
Softstuff accepts no responsibility for the use of information contained within this item. No warranties are implied or provided and it is advised that you have a working knowledge of the technologies contained, and we will not be held liable for system downtime, or data loss caused, unless we have specifically advised in writing to you that this is the case during the course of a support issue raised and accepted by us.
 

Copyright Softstuff Consulting 2007-2009