Mega Code Archive

 
Categories / Delphi / Examples
 

Striphtmltags

> Not sure about the exact syntax of html. Here's a > state-driven parser. With at least one bug :=) I forgot to handle one state completely.. 2 lines have been inserted below. regards, Robert > regards, > Robert Friberg > > > > type TParseState = > (tpsNormal,tpsInTag,tpsInQuote,tpsInEscape,tpsInComment); > function StripTags(inStr:string):string; > var state:TParseState; > idx : integer; > LenInStr : integer; > QuoteChar:char; > begin > state := tpsNormal; > Result := ''; > idx := 1; > LenInStr := Length(inStr); {avoid repeated caclulation} > while idx <= LenInStr do begin > case state of > tpsNormal : begin > if inStr[idx] = '<' then state := tpsInTag > else Result := Result + inStr[idx]; > end; > tpsInTag: begin > if inStr[idx] = '>' then state := tpsNormal > else if Copy(inStr, idx-1, 3) = '<!--' then begin > state := tpsInComment; > idx := idx + 2; > end > else if inStr[idx] in ['''','"'] then begin > QuoteChar := inStr[idx]; > state := tpsInQuote; > end; > end; > tpsInQuote: begin > if inStr[idx] = QuoteChar then state := tpsInTag > else if inStr[idx] = '\' then state := tpsInEscape; > end; > tpsInEscape: idx := idx + 1; state := tpsInQuote; {Just skip this character....} > tpsInComment: begin > if Copy(inStr,idx,3) = '-->' then begin > state := tpsNormal; > idx := idx + 2; > end; > end; > end; > idx := idx + 1; > end; > end;