Solved

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

Posted on 2014-10-17
9
278 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
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!

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

617 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