Solved

Encode simple text string to html format

Posted on 2004-04-27
4
2,329 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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