0 users online | 0 Guests and 0 Registered

»

ID #1068

Text zwischen zwei TAG's isolieren

procedure IsolateTextBetweentags(Const S: String; Tag1, Tag2: String; list:TStrings);
var
  pScan, pEnd, pTag1, pTag2: PChar;
  foundText: String;
  searchtext: String;
begin
  {Set up pointers we need for the search. HTML is not case sensitive, so
   we need to perform the search on a uppercased copy of S}
  searchtext := Uppercase(S);
  Tag1 := Uppercase( Tag1 );
  Tag2 := Uppercase( Tag2 );
  pTag1 := PChar(Tag1);
  pTag2 := PChar(Tag2);
  pScan := PChar(searchtext);
  repeat
    {Search for next occurence of Tag1}
    pScan:= StrPos( pScan, pTag1 );
    if pScan <> Nil then
    begin
      {Found one, hop over it, then search from that position forward for the next occurence of Tag2}
      Inc(pScan, Length( Tag1 ));
      pEnd := StrPos( pScan, pTag2 );
      if pEnd <> Nil then
      begin
        {Found start and end tag, isolate text between, add it to the list. We need to
         get the text from the original S, however, since we want the un-uppercased version!}
        SetString(foundText, Pchar(S) + (pScan- PChar(searchtext)), pEnd - pScan);
        list.Add( foundText );
        {Continue next search after the found end tag}
        pScan := pEnd + Length(tag2);
      end
      else
        {Error, no end tag found for start tag, abort}
        pScan := Nil;
    end;
  until pScan = Nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with opendialog1 do
  begin
    filter := 'HTML files|*.HTM; *.HTML';
    if execute then
    begin
      richedit1.PlainText := true;
      richedit1.lines.loadfromfile( filename );
      Listbox1.clear;
      {change brackets of HTML tags in the following line}
      IsolateTextBetweenTags( richedit1.text, '<title>', '</title>', Listbox1.Items );
    end;
  end;
end;

Tags: -

Related entries: -

Last update: 2010-07-18 18:22
Author: Rolf Warnecke
Revision: 1.0

{writePrintMsgTag} {writeSend2FriendMsgTag} {writePDFTag}
Please rate this FAQ:

Average rating: 0 (0 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Most popular FAQs RSS

  1. Display Resolution change
    (30 views)
  2. Scanned a Picture
    (13 views)

Latest FAQs RSS

  1. Scanned a Picture
    (2010-11-19 06:01)
  2. Display Resolution change
    (2010-02-09 18:01)