Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Delete Text From / To

Hi all,

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

<head>  to </head> including the tags ... I will then add a string there at that deleted position.

Hope you can help

thx

st3vo
Avatar of ST3VO
ST3VO
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Anyone please?
Not tested, and you might want to make sure my guessing at a big number for the size is okay...

var i,j,FileHandle: Integer;

FileHandle := FileOpen(FileName,fmShareDenyNone+fmOpenReadWrite)
FileRead(FileHandle,s,200000);
i := pos('<head>',s);
j := pos('</head>',s);
if (i>0) and (j>i) then
    begin
    s := Copy(s,1,i-1) + WhatYouWantToReplaceWith + Copy(s,j+7,200000);
    if FileWrite(FileHandle,s,length(s))<>length(s) then
        raise Exception.Create('write error');
    end;
Avatar of ST3VO

ASKER

Hmm...doesn't compile..error...readFile

Anyway..this is what I'm trying to do:

NOTE: {head.txt contains the code I need to put into the TMemo and it replaces the current code in the TMemo between the <head> and </head> tags ....note: head.txt also contains the <head> tags so the whole thing is replaced.}

Procedure TForm1.LoadAndReplaceCodeIntoTMemo;
var i,j,FileHandle: Integer;
    s:string;
 begin
FileHandle := FileOpen('head.txt',fmShareDenyNone+fmOpenReadWrite)
FileRead(FileHandle,s,200000);
i := pos('<head>',s);
j := pos('</head>',s);
if (i>0) and (j>i) then
    begin
    s := Copy(s,1,i-1) + 'head.txt' + Copy(s,j+7,200000);
    if FileWrite(FileHandle,s,length(s))<>length(s) then
        raise Exception.Create('write error');
    end;
You need something added to uses for those, but no matter, I was just trying to read the whole thing as one big string.  I'd think there would be some way of getting the memo as a string but I don't know what it is.  If nothing else, you could do:

s := '';
for i := 0 to Memo1.Items.Count-1 do
   s := s+Memo1.Items[i]+#13#10;

Then do the pos and Copy stuff.  Then write it to a file of bytes, unless there's a function I don't know about to put it back in the memo.  You could do LoadFromFile to get it back into a string list and assign it to the memo.


Avatar of ST3VO

ASKER

I don't need to read the whole memo.

I need to delete from <head> to </head> in the memo and replace the deleted with the contents of a file called 'head.txt'

hope this helps

thx!
Avatar of Emmanuel PASQUIER
almost the same as SearchBetweedAndDelete, and could be used for the same (as replace with '' is simple delete)

SearchBetweenAndReplace(Str, '<head>', '</head>' , ReplaceStr);

In all the functions you asked, the real trick is to use PosEx instead of Pos (to search after the initial token found), and to give special attention to the positions and length of the tokens, in order to do the right Copy
function SearchBetweenAndReplace(
               Var Str:String; const StrBefore,StrAfter :String;ReplaceStr:String='') :Boolean;
Var
 P,P2:Integer;
begin
 P:=Pos(StrBefore,Str);
 if P>0 Then
  begin
   P:=P+Length(StrBefore);
   P2:=PosEx(StrAfter,Str,P);
   Result:=P2>P;
   if Result Then 
    begin
     P2:=P2+Length(StrAfter);
     Str:=LeftStr(Str,P-1-Length(StrBefore))+ReplaceStr+Copy(Str,P2,Length(Str)-P2+1);
    end;
  end Else Result=False;
end;

Open in new window

Avatar of ST3VO

ASKER

Testing your function but I don't think I'm getting it right...

This is what I'm trying:

SearchBetweenAndReplace(Memo1.Text, '<head>', '</head>' , ' ');

Cannot comipile is as I'm getting error:

[DCC Error] Unit1.pas(784): E2197 Constant object cannot be passed as var parameter

Any ideas please?

That's the problem with Var parameter, you have first to copy them in a string variable to use them.

either do :

Var
 TempStr:String;
begin
 TempStr:=Memo1.Text;
 if SearchBetweenAndReplace(TempStr, '<head>', '</head>' , ' ') Then Memo1.Text:=TempStr;
end;

or you don't care about boolean result of your function (assuming it always succeed) and send the same string if not found
function SearchBetweenAndReplace(Str:String; const StrBefore,StrAfter :String;const ReplaceStr:String='') :String;
Var
 P,P2:Integer;
begin
 P:=Pos(StrBefore,Str);
 if P>0 Then
  begin
   P:=P+Length(StrBefore);
   P2:=PosEx(StrAfter,Str,P);
   if P2>P Then 
    begin
     P2:=P2+Length(StrAfter);
     Result:=LeftStr(Str,P-1-Length(StrBefore))+ReplaceStr+Copy(Str,P2,Length(Str)-P2+1);
    end Else Result=Str;
  end Else Result=Str;
end;

Open in new window

Avatar of ST3VO

ASKER

Hi epasquier,

That's works great ... but just been trying to insert something where the code was taken off and it doesn't.

[quote] ... I will then add a string there at that deleted position.[/quote]

Tried this but it was added to the end of the memo text

if SearchBetweenAndReplace(TempStr, '<head>', '</head>' , ' ') Then
   begin
     Memo1.Text:=TempStr;
     memo1.Lines.add('HEAD TAGS GO HERE');
   end;

Any ideas pls?


ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

Perfect!!!! Thanks :o)