Solved

Replace string inside a HTML and TXT file with content of Memo

Posted on 2014-10-17
9
274 Views
Last Modified: 2014-10-28
Hi,

I need to replace a string inside a HTML and a TXT file with the content of TMemo.
Here the formatting (soft and hard linebreaks) inside the TMemo must be preserved.
Inside the HTML or TXT file the replacement-string is formatted like %myreplacementcode%.

I've tried using StringReplace but all content inside the memo is than placed on a single line.

Any suggestions?
0
Comment
Question by:Delphiwizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 40386528
Can you show some code, just a few lines how you tried with StringReplace() and it all ended up on a single line.
0
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 500 total points
ID: 40386552
The problem with HTML should be obvious: A line break (lf, crlf) in TMemo is not interpreted in HTML as line break. You need to parse those. E.g. by using <br> or a <p>.
0
 

Author Comment

by:Delphiwizard
ID: 40386823
I started simple:
StringReplace(vHTMLString,'%MyMemoContent%', Memo1.Lines.Text, [rfReplaceAll, rfIgnoreCase]);

Open in new window

Then I converted the Memo to String with following function:
function MemoToStr(Sender: TObject) : String;
var i : Integer;
begin
  for i := 0 to (Sender AS TMemo).Lines.Count -1 do
  begin
    if (i <> 0) then
      Result := Result + chr(13);
    Result := Result + Copy((Sender AS TMemo).Lines.Strings[i], 1, Length((Sender AS TMemo).Lines.Strings[i]) -2);
  end;
end;

Open in new window

StringReplace(vHTMLString,'%MyMemoContent%', MemoToStr(Memo1), [rfReplaceAll, rfIgnoreCase]);

Open in new window


So I guess I need to start working on a different kind of parsing.
Any suggestions are more then welkom.
0
Industry Leaders: 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!

 

Author Comment

by:Delphiwizard
ID: 40393722
This is the way to do it.
function MemoToStrHTML(Sender: TObject) : String;
var i : Integer;
begin
  for i := 0 to (Sender AS TMemo).Lines.Count -1 do
  begin
    if (i <> 0) then
      Result := Result + '<br>';
    Result := Result + (Sender AS TMemo).Lines.Strings[i];
  end;
end;

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 40394336
I've requested that this question be closed as follows:

Accepted answer: 0 points for Delphiwizard's comment #a40393722

for the following reason:

Provided my own answer.
0
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 40393839
Nope, a helper should be the first choice:

type
  StringsHelper = class helper for TStrings
  public
    function ToHTML: string;
  end;

implementation

function StringsHelper.ToHTML: string;
var
  I: Integer;
begin
  for i := 0 to Self.Count - 1 do
  begin
    if (i <> 0) then
      Result := Result + '<br>';
    Result := Result + Self.Strings[i];
  end;
end;

Open in new window


Then it's a nice Memo.Lines.ToHTML.
0
 

Author Comment

by:Delphiwizard
ID: 40394340
ste5an:
Can you show me how to implement this in real life?
Is it sufficient to add your code to the main unit of the application?
0
 
LVL 34

Expert Comment

by:ste5an
ID: 40394368
Here is a single file sample. Just fill in your code:

unit Unit8;

interface

uses
  System.Classes,
  Vcl.Controls, Vcl.Dialogs, Vcl.Forms, Vcl.StdCtrls;

type
  TForm8 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

  MemoHelper = class helper for TMemo
  public
    function ToHTML: string;
  end;

  StringsHelper = class helper for TStrings
  public
    function ToHTML: string;
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

procedure TForm8.Button1Click(Sender: TObject);
begin
  ShowMessage(Memo1.ToHTML);
  ShowMessage(Memo1.Lines.ToHTML);
end;

{ MemoHelper }

function MemoHelper.ToHTML: string;
begin
  Result := 'HTML from TMemo';
end;

{ StringsHelper }

function StringsHelper.ToHTML: string;
begin
  Result := 'HTML from TStrings';
end;

end.

Open in new window


The helper classes should be placed in separate files.

Either you a fan of you own big helper file, then you only need one to place in the uses. Or one file per helper base class. So I would place them into Vcl.StdCtrls.Helper.pas and System.Classes.Helper.pas. Or one file per helper class Helper.Memo.pas and Helper.Strings.pas.

The number and naming depends on how you want to organize your namespaces.
0
 

Author Comment

by:Delphiwizard
ID: 40408136
Thank you ste5an
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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