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
ST3VOAsked:
Who is Participating?
 
Emmanuel PASQUIERConnect With a Mentor Freelance Project ManagerCommented:
it is the last parameter you have to use :

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

I also added a functionality to keep the string before and after (the tags in your case), on option. By default, if you do not specify the boolean parameter, it should work like the old one. Otherwise :


SearchBetweenAndReplace(TempStr, '<head>', '</head>' , 'HEAD TAGS GO HERE', True)

IS EQUIVALENT TO

SearchBetweenAndReplace(TempStr, '<head>', '</head>' , '<head>HEAD TAGS GO HERE</head>')
function SearchBetweenAndReplace(Str:String; const StrBefore,StrAfter :String;const ReplaceStr:String=''; KeepBeforeAndAfter:Boolean=False) :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
     if Not KeepBeforeAndAfter Then 
      begin
       P:=P-Length(StrBefore);
       P2:=P2+Length(StrAfter);
      end;
     Result:=LeftStr(Str,P-1)+ReplaceStr+Copy(Str,P2,Length(Str)-P2+1);
    end Else Result=Str;
  end Else Result=Str;
end;

Open in new window

0
 
ST3VOAuthor Commented:
Anyone please?
0
 
SuperdaveCommented:
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;
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
ST3VOAuthor Commented:
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;
0
 
SuperdaveCommented:
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.


0
 
ST3VOAuthor Commented:
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!
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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

0
 
ST3VOAuthor Commented:
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?

0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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

0
 
ST3VOAuthor Commented:
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?


0
 
ST3VOAuthor Commented:
Perfect!!!! Thanks :o)
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.