Solved

Encode simple text string to html format

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

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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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