Delphi source code

» Remove HTML tags from a string

How do the search engines see your web pages? You can find it out by using the function StripHTML, that strips all HTML tags from a given text- string.

For example, the string:   <b><i>Important</i></b> notes
must be converted to: Important notes

 
function StripHTML(S: string): string;
var
  TagBegin, TagEnd, TagLength: integer;
begin
  TagBegin := Pos( '<', S);  // search position of first <

  while (TagBegin > 0) do begin  // while there is a < in S
    TagEnd := Pos('>', S);  // find the matching >
    TagLength := TagEnd - TagBegin + 1;
    Delete(S, TagBegin, TagLength); // delete the tag
    TagBegin:= Pos( '<', S);        // search for next <
  end;
  
  Result := S;                // give the result
end;

Example of usage:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo2.Text := StripHTML(Memo1.Text);
end;

TOP   DelphiLand Club Membership  DC Library  Forum  Forum Archives  
Crash Course Delphi   Tips  Source Code  Downloads  Links

© Copyright 1999-2018 
DelphiLand