Solved

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

Posted on 2014-10-17
9
259 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
  • 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 32

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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 32

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 32

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now