Copy and Text From / To

Ho all,

I have an html page and I need to copy all html code from:

<head>  to </head>  //...this includes the head tags and put it in a string variable.

Hope you can help

thx

st3vo
ST3VOAsked:
Who is Participating?
 
Dagan HooverDeveloperCommented:
function CopyText(const S: String; FromText, ToText: String): String;
var
  PosStart,
  PosEnd,
  PosLen : Integer;
begin
  Result := '';
  PosStart := Pos(S,FromText);
  PosEnd := Pos(S,ToText);
  PosLen := PosEnd - PosStart;
  If PosLen < 1 Then Exit;
  Result := Copy(S,PosStart,PosLen + Length(ToText));
end;

MyString := CopyText(MyHTMLData,'<head>','</head>');
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
ThievingSix has a working function in this case but for generic use I recommend the one bellow. Main difference is if ToText is a substring of FromText or if ToText can appear in given string before FromText, CopyText will not work properly. He should have used PosEx to find PosEnd only after the FromText

usage is same :

MyString :=SearchBetweenAndCopy(MyHTMLData,'<head>','</head>');
function SearchBetweenAndCopy(Str:String; const StrBefore,StrAfter :String) :String;
Var
 P,P2:Integer;
begin
 P:=Pos(StrBefore,Str);
 Result='';
 if P>0 Then
  begin
   P:=P+Length(StrBefore);
   P2:=PosEx(StrAfter,Str,P);
   if P2>P Then 
    begin
     P:=P-Length(StrBefore);
     P2:=P2+Length(StrAfter);
     Result:=Copy(Str,P,P2-P);
    end;
  end;
end;

Open in new window

0
 
ST3VOAuthor Commented:
Perfect! Thanks!!!
0
 
ST3VOAuthor Commented:
Hi epasquier,

Yes, you where right, it didn't work ... I had already assigned the points though :o/

Sorry about that!

If you can help, I now need to delete instead of copy ...

I've opened a question here:

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24992364.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.