?
Solved

Delete Text From / To

Posted on 2009-12-20
11
Medium Priority
?
292 Views
1 Endorsement
Last Modified: 2012-05-08
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
1
Comment
Question by:ST3VO
  • 6
  • 3
  • 2
11 Comments
 

Author Comment

by:ST3VO
ID: 26090287
Anyone please?
0
 
LVL 13

Expert Comment

by:Superdave
ID: 26090681
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
 

Author Comment

by:ST3VO
ID: 26090740
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Superdave
ID: 26090796
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
 

Author Comment

by:ST3VO
ID: 26090927
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
 
LVL 25

Expert Comment

by:epasquier
ID: 26091690
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
 

Author Comment

by:ST3VO
ID: 26099019
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
 
LVL 25

Expert Comment

by:epasquier
ID: 26100635
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
 

Author Comment

by:ST3VO
ID: 26100769
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
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 26100974
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
 

Author Closing Comment

by:ST3VO
ID: 31668227
Perfect!!!! Thanks :o)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question