Solved

Encode simple text string to html format

Posted on 2004-04-27
4
2,203 Views
Last Modified: 2008-04-14
Just need a sample of Delphi 7 code to convert a text (I read it from a DB) in html code.
I mean, special character of the text should be converted to correct entities
For example < should become &gt;  or & should become &amp; etc....
In fact the equivalent of the ASP server.htmlencode method

var
 S:string;
begin
 .../... read here from some source to S
 S := ????(S)
end
0
Comment
Question by:LeTay
4 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10928831

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := StringReplace(Edit1.Text, '<', '&lt', [rfReplaceAll]);
end;
0
 
LVL 6

Expert Comment

by:rbohac
ID: 10928874
You will most likely have to create the function yourself

function ChangeTextToHTML(S:String):String;
begin
  Result := S;
  Result := StringReplace(S,'&','&amp;',[rfReplaceAll]);
  Result := StringReplace(S,'<','&lt;',[rfReplaceAll]);
  Result := StringReplace(S,'>','&gt;',[rfReplaceAll]);
  //the rest of the ones you need to convert
end;

You could also encode the whole thing

function ChangeTextToHTML2(S:String):String;
var x:Integer;
begin
  Result := '';
  for x:=1 to Length(s) do
    Result := Result + '&#'+IntToStr(Ord(s[x]))+';';
end;
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10929095

Just another example....

function HTMLEncode(S: String): String;
var  dwLength:      Integer;
     lpszEnc:       PChar;
     lpszEncode:    PChar;
     lpszPos:       PChar;
const
  ENCODE_CHARS:     PChar =  #10#13#34#38#60#62#0;
  ENCODE_VALS:      Array [0..5] of PChar = ('<br>', #0, '&quot;', '&amp;', '&lt;', '&gt');
begin

  // Allocate memory for the parsing (the largest encoding is 6 bytes from a single byte)
  lpszEncode:=AllocMem(Succ(6 * Length(S)));

  // Set starting output
  lpszPos:=lpszEncode;

  // Resource protection
  try
     // Walk the string
     for dwLength:=1 to Length(S) do
     begin
        lpszEnc:=StrScan(ENCODE_CHARS, S[dwLength]);
        if Assigned(lpszEnc) then
        begin
           StrCopy(lpszPos, ENCODE_VALS[lpszEnc-ENCODE_CHARS]);
           lpszPos:=StrEnd(lpszPos);
        end
        else
        begin
           lpszPos^:=S[dwLength];
           Inc(lpszPos);
        end;
     end;
  finally
     // Set result
     result:=lpszEncode;
     // Free memory
     FreeMem(lpszEncode);
  end;

end;

-------------

Russell
0
 
LVL 2

Accepted Solution

by:
Deti earned 500 total points
ID: 10931549
Here you have some ready code (100% working):

type
  TEncions = packed record
    A: Char;
    B: PChar;
  end;

Encions: array [0..85] of TEncions =
  ((A: '&'; B: '&amp;'),
   (A: '&#729;'; B: '&#255;'),
   (A: '"'; B: '&quot;'),
   (A: '&#341;'; B: '&agrave;'),
   (A: 'ç'; B: '&ccedil;'),
   (A: 'é'; B: '&eacute;'),
   (A: '&#269;'; B: '&egrave;'),
   (A: '&#281;'; B: '&ecirc;'),
   (A: '&#367;'; B: '&ugrave;'),
   (A: 'ë'; B: '&euml;'),
   (A: '<'; B: '&lt;'),
   (A: '>'; B: '&gt;'),
   (A: '^'; B: '&#136;'),
   (A: '~'; B: '&#152;'),
   (A: '&#321;'; B: '&#163;'),
   (A: '§'; B: '&#167;'),
   (A: '°'; B: '&#176;'),
   (A: '&#731;'; B: '&#178;'),
   (A: '&#322;'; B: '&#179;'),
   (A: '&#317;'; B: '&#188;'),
   (A: '&#733;'; B: '&#189;'),
   (A: '&#380;'; B: '&#191;'),
   (A: '&#340;'; B: '&#192;'),
   (A: 'Á'; B: '&#193;'),
   (A: 'Â'; B: '&#194;'),
   (A: '&#258;'; B: '&#195;'),
   (A: 'Ä'; B: '&#196;'),
   (A: '&#313;'; B: '&#197;'),
   (A: '&#262;'; B: '&#198;'),
   (A: 'Ç'; B: '&#199;'),
   (A: '&#268;'; B: '&#200;'),
   (A: 'É'; B: '&#201;'),
   (A: '&#280;'; B: '&#202;'),
   (A: 'Ë'; B: '&#203;'),
   (A: '&#282;'; B: '&#204;'),
   (A: 'Í'; B: '&#205;'),
   (A: 'Î'; B: '&#206;'),
   (A: '&#270;'; B: '&#207;'),
   (A: '&#323;'; B: '&#209;'),
   (A: '&#327;'; B: '&#210;'),
   (A: 'Ó'; B: '&#211;'),
   (A: 'Ô'; B: '&#212;'),
   (A: '&#336;'; B: '&#213;'),
   (A: 'Ö'; B: '&#214;'),
   (A: '&#366;'; B: '&#217;'),
   (A: 'Ú'; B: '&#218;'),
   (A: '&#368;'; B: '&#219;'),
   (A: 'Ü'; B: '&#220;'),
   (A: 'Ý'; B: '&#221;'),
   (A: 'ß'; B: '&#223;'),
   (A: '&#341;'; B: '&#224;'),
   (A: 'á'; B: '&#225;'),
   (A: 'â'; B: '&#226;'),
   (A: '&#259;'; B: '&#227;'),
   (A: 'ä'; B: '&#228;'),
   (A: '&#314;'; B: '&#229;'),
   (A: '&#263;'; B: '&#230;'),
   (A: 'ç'; B: '&#231;'),
   (A: '&#269;'; B: '&#232;'),
   (A: 'é'; B: '&#233;'),
   (A: '&#281;'; B: '&#234;'),
   (A: 'ë'; B: '&#235;'),
   (A: '&#283;'; B: '&#236;'),
   (A: 'í'; B: '&#237;'),
   (A: 'î'; B: '&#238;'),
   (A: '&#271;'; B: '&#239;'),
   (A: '&#324;'; B: '&#241;'),
   (A: '&#328;'; B: '&#242;'),
   (A: 'ó'; B: '&#243;'),
   (A: 'ô'; B: '&#244;'),
   (A: '&#337;'; B: '&#245;'),
   (A: 'ö'; B: '&#246;'),
   (A: '÷'; B: '&#247;'),
   (A: '&#367;'; B: '&#249;'),
   (A: 'ú'; B: '&#250;'),
   (A: '&#369;'; B: '&#251;'),
   (A: 'ü'; B: '&#252;'),
   (A: 'ý'; B: '&#253;'),
   (A: '©'; B: '&#169;'),
   (A: '«'; B: '&#171;'),
   (A: '®'; B: '&#174;'),
   (A: '°'; B: '&#176;'),
   (A: '±'; B: '&#177;'),
   (A: 'µ'; B: '&#181;'),
   (A: '»'; B: '&#187;'),
   (A: '™'; B: '&#8482;'));



And main function:


function ConvertToEncion(s: string): string;
var
  i: Integer;
begin
  Result := s;
  for i := Low(Encions) to High(Encions) do
    Result := StringReplace(Result, Encions[i].A, Encions[i].B, [rfReplaceAll, rfIgnoreCase]);
end;


Maybe you want to back :)


function ConvertFromEncion(s: string): string;
var
  i: Integer;
begin
  Result := s;
  for i := Low(Encions) to High(Encions) do
    Result := StringReplace(Result, Encions[i].B, Encions[i].A, [rfReplaceAll, rfIgnoreCase]);
end;
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

22 Experts available now in Live!

Get 1:1 Help Now