Important alert: (current site time 7/15/2013 7:49:45 PM EDT)
 

VB icon

A relational technique to strip the HTML tags out of a string

Email
Submitted on: 7/29/2000 9:56:49 PM
By: Umachandar  
Level: Intermediate
User Rating: Unrated
Compatibility: SQL Server 7.0, SQL Server 6.5 and earlier
Views: 18277
 
     A relational technique to strip the HTML tags out of a string. This solution demonstrates how to use simple tables & search functions effectively in SQL Server to solve procedural / iterative problems.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: A relational technique to strip the HTML tags out of a string
-- Description:A relational technique to strip the HTML tags out of a string. This solution demonstrates how to use simple tables & search functions effectively in SQL Server to solve procedural / iterative problems.
-- By: Umachandar
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=197&lngWId=5--for details.--**************************************

-- This table contains the tags to be replaced. The % in <head%>
-- 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 ( '<html>' )
insert #html values ( '<head%>' )
insert #html values ( '<title%>' )
insert #html values ( '<link%>' )
insert #html values ( '</title>' )
insert #html values ( '</head>' )
insert #html values ( '<body%>' )
insert #html values ( '</html>' )
go
-- A simple table with the HTML strings
create table #t ( id tinyint identity , string varchar(255) ) 
insert #t values (
'<HTML><HEAD><TITLE>Some Name</TITLE>
<LINK REL="stylesheet" HREF="/style.css" TYPE="text/css" ></HEAD>
<BODY BGCOLOR="FFFFFF" VLINK="#444444">
Some HTML text after the body</HTML>'
)
insert #t values (
'<HTML><HEAD><TITLE>Another Name</TITLE>
<LINK REL="stylesheet" HREF="/style.css"></HEAD>
<BODY BGCOLOR="FFFFFF" VLINK="#444444">Another HTML text after the body</HTML>'
)
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


Other 133 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/18/2006 8:21:14 AM

nice
(If this comment was disrespectful, please report it.)

 
1/3/2008 1:09:01 AMbilly

good job
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.