When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Friday, October 01, 1999

Stripping HTML Tags

By Abd Shomad


Note from Scott Mitchell, webmaster:
Ah, it's nice to see an article by Abd. It's been too long since his last article! This nifty little script will strip all the HTML tags from out of a string.


Here's a way to Strip HTML Tags. I found this snippet of code is very usefull when you want to output either HTML or PLAIN TEXT with your ASPMail/CDONTS component. (To learn more about sending emails through an ASP page, be sure to read the Email FAQ Category section at ASPFAQs.com!)

- continued -

Here's the code:

' Function Strip All HTML Tags ' Author Abd Shomad: ' http://www.4guysfromrolla.com/webtech/abdshomad.shtml Function StripHTMLTag(ByVal sText) StripHTMLTag = "" fFound = False Do While InStr(sText, "<") fFound = True StripHTMLTag = StripHTMLTag & " " & Left(sText, InStr(sText, "<")-1) sText = MID(sText, InStr(sText, ">") + 1) Loop StripHTMLTag = StripHTMLTag & sText If Not fFound Then StripHTMLTag = sText End Function

For this to work, your HTML tags must be VALID HTML tags! If you have any < or >, that do not denote the beginning of an HTML tag, you need to repalce < with &lt; and > with &gt; (for example, if you have in your HTML string: "The following is a true statement: 5 < 10", you would need to replace the < with &lt;.)

A More Updated Version
The above function is a bit buggy and picky, requiring a matching number of < and > signs. A cleaner and more flexible HTML stripping function can be found at: Stripping HTML Tags Using Regular Expressions.

Happy Programming!


Attachments:

  • The source code for the StripHTMLTag in text format

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article