HTML Strip function

BlitzMax Forums/BlitzMax Programming/HTML Strip function

nawi(Posted 2005) [#1]
Can somebody write a HTML strip function for me (gimme da codez)?


Robert(Posted 2005) [#2]
Here you go - this is a very simple implementation though, and it does not strip comments or ignore tag characters in strings.

Print stripTags("<html><head><title>This Is A title</title></head><body>Whatever</body></html>")

Function stripTags:String(text:String)
	
	Local tagDepth
	Local output:String
	
	For Local i=0 Until text.length
		If text[i]=Asc("<") tagDepth :+ 1
		
		
		If tagDepth=0
			output = output + Chr(text[i])
		End If
		
		If text[i]=Asc(">") tagDepth :- 1
	Next
	
	Return output
	
End Function



nawi(Posted 2005) [#3]
Damn. Just when I finished mine :). Well here's my version, somewhat complicated:

Function HTMLStrip$(Line$,DelDoubleSpaces=0)
	Local T
	For Local L = 1 To Len(Line$)
		If Mid$(Line$,L,1) = "<" Then
			T = L
			Repeat
				T = T + 1
			Until (Mid$(Line$,T,1) = ">") Or (T = Len(Line$))
			Line$ = Line$.Replace(Mid$(Line$,L,T-L+1),"")
			L = 0
		EndIf
	Next
	If DelDoubleSpaces = 0 Then
		Return Line$
	Else
		Return Line$.Replace("  "," ")
	EndIf
End Function



Suco-X(Posted 2005) [#4]
An now the fastest way ;)

Function StripTags:String(Text:String)
	
	Local Opened:Int    = False
	Local Asc1:Int      = Asc("<")
	Local Asc2:Int      = Asc(">")
	Local j:Int         = 0
	Local Result:Byte[Text.Length]
	
	For Local i:Int = 0 Until Text.Length	
		If Text[i] = Asc1
			Opened = True
			Continue
		ElseIf Text[i] = Asc2
			Opened = False
			Continue
		EndIf
		If Opened = True
			Continue
		EndIf
		
		Result[j] = Text[i]			
		j:+1
	Next 
	
	Return string.FromBytes(Result, j)
End Function 

Mfg Suco