stripping HTML tags

S

shank

I'm querying a text field with an 8000 character limit. The text also
contains HTML tags like <p> <br> and more. Is there a way to strip all HTML
tags in the resulting recordset, or do I have to replace each tag
individually?

thanks
 
E

Evertjan.

shank wrote on 10 jul 2004 in microsoft.public.inetserver.asp.general:
I'm querying a text field with an 8000 character limit. The text also
contains HTML tags like <p> <br> and more. Is there a way to strip all
HTML tags in the resulting recordset, or do I have to replace each tag
individually?

Using [serverside] jscript:

YourResult = yourField.replace(/<[^>]*>/g,' ')

Replacing tags with a space, because some tags replace a space in HTML.

This is not perfect.

<input value='a "false" >'>
will give a wrong result
 
C

Chris Hohmann

Evertjan. said:
shank wrote on 10 jul 2004 in microsoft.public.inetserver.asp.general:
I'm querying a text field with an 8000 character limit. The text also
contains HTML tags like <p> <br> and more. Is there a way to strip all
HTML tags in the resulting recordset, or do I have to replace each tag
individually?

Using [serverside] jscript:

YourResult = yourField.replace(/<[^>]*>/g,' ')

Replacing tags with a space, because some tags replace a space in HTML.

This is not perfect.

<input value='a "false" >'>
will give a wrong result

Of course the above is not valid HTML, so your code is fine. :)
 
A

Alex Kail

I've always had great luck with this function:

Function StripHTML(strText)

'Strips the HTML tags from the text passed (strText). AK

'Checking for HTML tag opening brackets
If InStr(1, strText, "<") = 0 Then
'No opening brackets, no chance of HTML being used
'So just return the original string. AK
StripHTML = strText
Exit Function 'Abort
End If

'This takes the potential HTML text passed and
'splits it up into an array to loop through. AK
Dim arySplit, lngLoop, lngStart, strOutput
arySplit = Split(strText, "<")

'Determine where to start parsing
If Len(arySplit(0)) > 0 Then
lngStart = 1 'First character is an opening tag
Else
lngStart = 0
End If

'Taking out the HTML opening tags
For lngLoop = lngStart To UBound(arySplit)
If InStr(arySplit(lngLoop), ">") Then
arySplit(lngLoop) = Mid(arySplit(lngLoop),
InStr(arySplit(lngLoop), ">") + 1)
Else
arySplit(lngLoop) = "<" & arySplit(lngLoop)
End If
Next

strOutput = Join(arySplit, "")
strOutput = Mid(strOutput, 2 - lngStart)
strOutput = Replace(strOutput, ">", ">")
strOutput = Replace(strOutput, "<", "<")

StripHTML = strOutput

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top