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!)
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 < and > with
> (for example, if you have in your HTML string:
"The following is a true statement: 5 < 10", you would need to replace the < with <.)
| 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