I've been working with strings a lot lately and have some functions that might be useful to you. I made a quick example program that just finds the first tag on each line.
remstart
24th JAN 2013 - HTML Parser - by OBese87
---------------------------------------------------------------------------------------------------
- BRACKET ASCII CODES -
Double Quote = 34
Single Quote = 39
Left Parenthesis = 40
Right Parenthesis = 41
Left Chevron = 60
Right Chevron = 62
Left Square Bracket = 91
Right Square Bracket = 93
Left Brace = 123
Right Brace = 125
- MORE ASCII CODES -
Space = 32
---------------------------------------------------------------------------------------------------
remend
dq$ = chr$(34) :`Double Quotes ""
sq$ = chr$(39) :`Single Quotes ''
lp$ = chr$(40) : rp$ = chr$(41) :`Parentheses ()
lc$ = chr$(60) : rc$ = chr$(62) :`Chevrons <>
ls$ = chr$(91) : rs$ = chr$(93) :`Square Brackets []
lb$ = chr$(123) : rb$ = chr$(125) :`Braces {}
sp$ = chr$(32) :`Space
open to read 1,"test.htm"
while file end(1)=0
read string 1,a$
openTagA = chrPos(a$,lc$)
if openTagA
openTagB = chrPos(right$(a$,len(a$)-openTagA), rc$)
within$ = midleft$(a$, openTagA, openTagB-openTagA)
tag$ = split$(within$,sp$,0)
param$ = split$(within$,sp$,1)
if param$=tag$ then param$=" "
print "Plain String: ";a$
print "Content of first tag: "; within$
print "Tag Class: "; tag$
print "Tag Parameters: "; param$
print
endif
endwhile
close file 1
end
rem --- Functions ---------------------------------------------------------------------------------
rem Takes a string that opened with open$ and returns the position at which ALL instances of open$
rem inside the scope have been closed by close$.
function closePos(a$,open$,close$)
open = 1 :`We didn't pass the first open bracket in the string so log it.
inso = 0 : insc = 0 :`return instance 'ins'
while open!0
opos = strPosIns(a$,open$,inso) :`Open Bracket
cpos = strPosIns(a$,close$,insc) :`Close Bracket
if opos
if opos < cpos or cpos=0 then inc open : inc inso
endif
if cpos
if cpos < opos or opos=0 then dec open : inc insc
endif
endwhile
endfunction cpos
`//
function chrPos(source$,c$)
for x = 1 to len(source$)
if mid$(source$,x) = c$ then exitfunction x
next x
endfunction 0
`//
rem Returns the position of instance 'i' of f$ within source$.
function chrPosIns(source$,f$,i)
for x = 1 to len(source$)
if mid$(source$,x) = f$
if i=0 then exitfunction x else dec i
endif
next x
endfunction 0
`//
function strPos(source$,s$)
for x = 1 to len(source$)
if midleft$(source$,x,len(s$)) = s$ then exitfunction x
next x
endfunction 0
`//
rem Returns the start-point of instance 'i' of f$ within source$.
function strPosIns(source$,f$,i)
for x = 1 to len(source$)
if midleft$(source$,x,len(f$)) = f$
if i=0 then exitfunction x else dec i
endif
next x
endfunction 0
`//
rem Returns a section of the source string starting at 'pos' and extending 'wid' characters.
function midleft$(a$,pos,wid)
a$ = right$(left$(a$,pos+wid),wid)
endfunction a$
`//
rem Returns the left- or right-most part of a string split at 'find$'.
rem 'find$' itself is omitted from the return string.
rem To split the source string at the first instance of 'find$':-
rem give 'part' a value of 0 (to return left half) or 1 (to return right half).
rem If you wish to split the string at a further instance of 'find$' simply increase
rem 'part' by 2 for every instance you wish to skip.
rem If the search fails the source string is returned.
function split$(s$,f$,part)
for x = 0 to len(s$)-len(f$)
if midleft$(s$,x,len(f$)) = f$
select part
case 0 : exitfunction left$(s$,x) : endcase
case 1 : exitfunction right$(s$,len(s$)-len(f$)-x) : endcase
case default : s$ = midleft$(s$,x+1,len(s$)) : dec part,2 : endcase
endselect
endif
next x
endfunction s$
`//
I feel like recursion might work well here. Here's a rough example:
max = 10
dim a$(max)
a$(0) = "PRINT Hello!"
a$(1) = "COLOR 255"
a$(2) = "TAB"
a$(3) = "PRINT Bonjour!"
a$(4) = "COLOR 65535"
a$(5) = "PRINT More Colours!"
a$(6) = "/COLOR"
a$(7) = "/TAB"
a$(8) = "PRINT Back out again?"
a$(9) = "/COLOR"
a$(10) = "PRINT Guten Tag!"
dim fileEnd(0)
fileEnd(0) = max
dim y(0)
y(0) = 160
readLn(0,200,-2)
wait key
end
function readLn(i,x,color)
for n = i to fileEnd(0)
n$ = a$(n)
ink -2,0
print n;". ";n$
rem get command
com$ = split$(n$," ",0)
select com$
case "PRINT"
ink color,0
a$ = split$(n$," ",1)
text x,y(0),a$
y(0) = y(0)+16
endcase
case "COLOR"
subcolor = val(split$(n$," ",1))
n = readLn(n+1,x,subcolor)
endcase
case "/COLOR" : exitfunction n : endcase
case "TAB"
n = readLn(n+1,x+20,color)
endcase
case "/TAB" : exitfunction n : endcase
endselect
next n
endfunction n
`//
rem Returns the left- or right-most part of a string split at 'find$'.
rem 'find$' itself is omitted from the return string.
rem To split the source string at the first instance of 'find$':-
rem give 'part' a value of 0 (to return left half) or 1 (to return right half).
rem If you wish to split the string at a further instance of 'find$' simply increase
rem 'part' by 2 for every instance you wish to skip.
rem If the search fails the source string is returned.
function split$(s$,f$,part)
for x = 0 to len(s$)-len(f$)
if midleft$(s$,x,len(f$)) = f$
select part
case 0 : exitfunction left$(s$,x) : endcase
case 1 : exitfunction right$(s$,len(s$)-len(f$)-x) : endcase
case default : s$ = midleft$(s$,x+1,len(s$)) : dec part,2 : endcase
endselect
endif
next x
endfunction s$
`//
rem Returns a section of the source string starting at 'pos' and extending 'wid' characters.
function midleft$(a$,pos,wid)
a$ = right$(left$(a$,pos+wid),wid)
endfunction a$
`//
It's kinda neat actually for a simple thing.
[edit]
Here's an example that reads from an external file:
open to read 1,"blarg.txt"
max = 99
dim a$(max)
lines = 0
while file end(1) =0
inc lines
read string 1,a$(lines)
endwhile
dim fileEnd(0)
fileEnd(0) = lines
dim y(0)
y(0) = 0
readLn(0,0,-2)
wait key
end
function readLn(i,x,color)
for n = i to fileEnd(0)
n$ = a$(n)
` ink -2,0
` print n;". ";n$
rem get command
com$ = upper$(split$(n$," ",0))
select com$
case "PRINT"
ink color,0
a$ = split$(n$," ",1)
text x,y(0),a$
y(0) = y(0)+16
endcase
case "COLOR"
subcolor = val(split$(n$," ",1))
n = readLn(n+1,x,subcolor)
endcase
case "/COLOR" : exitfunction n : endcase
case "TAB"
n = readLn(n+1,x+20,color)
endcase
case "/TAB" : exitfunction n : endcase
endselect
next n
endfunction n
`//
rem Returns the left- or right-most part of a string split at 'find$'.
rem 'find$' itself is omitted from the return string.
rem To split the source string at the first instance of 'find$':-
rem give 'part' a value of 0 (to return left half) or 1 (to return right half).
rem If you wish to split the string at a further instance of 'find$' simply increase
rem 'part' by 2 for every instance you wish to skip.
rem If the search fails the source string is returned.
function split$(s$,f$,part)
for x = 0 to len(s$)-len(f$)
if midleft$(s$,x,len(f$)) = f$
select part
case 0 : exitfunction left$(s$,x) : endcase
case 1 : exitfunction right$(s$,len(s$)-len(f$)-x) : endcase
case default : s$ = midleft$(s$,x+1,len(s$)) : dec part,2 : endcase
endselect
endif
next x
endfunction s$
`//
rem Returns a section of the source string starting at 'pos' and extending 'wid' characters.
function midleft$(a$,pos,wid)
a$ = right$(left$(a$,pos+wid),wid)
endfunction a$
`//
And he's my "program":
print Hello World!
color 16711680
print IMPORTANT THINGS IN RED!!!
tab
Print Right, now lets... oh it's still red! xD
color 65535
Print That's Better, now where was I? Oh let's start a new tab...
tab
print Right! This time we are going to get the important stuff done.
Print I would make a list but... that's not in the commands.
/tab
/color
print I see why html is moving away from using <FONT> and the like and
tab
print using style-sheets instead. All these tags are messy!
/tab
/tab
/color
print Okay so probably not as messy as this but... oh we're back to normal.
print How boring... well, see ya!
Save that as a text file or make your own!
[edit]
Oh bugger it doesn't work in DBP

Very strange.
Well I've attached a screenshot. -->
"I am a big dumb babby!

" - TheComet