Link to home
Start Free TrialLog in
Avatar of AndersWP
AndersWP

asked on

Text2HTML function

Does anyone know of a 'Text2HTML function', i.e. a function that will take a string as input and as result return the same string, but now with all HTML-significant characters replaced by their HTML-safe equivalents, e.g. '<' -> '&lt', '&' -> '&amp' etc.

The function / procedure must be usable from a Delphi program. Though the function is not too complicated, I would rather not invent this deep plate again, since I am sure someone has already done it.

Regards,
AndersWP
Avatar of rwilson032697
rwilson032697

listening
On the contrary, this function is not so easy to write because you must make the distinction between html significant characters, and html alike characters embbedded in text.

I did the reverse functionnality you speak about, htlm to text in order to analyze pages, and I had to write a parser.

and Even like that, I don't get 100% of success because the browsers allow syntax that does not exactly fulfil the html specifications

regards, Marc
Avatar of AndersWP

ASKER

My needs are a bit simpler, since I can assume that my text does not contain any HTML tags. So I would think that a relatively simple substitution would do the trick.

Regards,
AndersWP
Although I don't have all of the tags in front of me, here is a start for you.

function Text2HTML(cIn:string):string;
const
   aIn  : array[0..2] of string =  
            ( '>','<','&');
   aOut : array[0..2] of string =
            ( '&gt','&lt','&amp');
var
   y,j,nSize : integer;
   cChar     : string;
begin
   result := '';
   nSize  := length(cIn);

   for j := 1 to nSize do
   begin
      cChar := copy(cIn,j,1);
      for y := 0 to 2 do
      begin
         if cChar=aIn[y] then
         begin
            cChar := aOut[y];
            break;
         end;
      end;
      result := result+cChar;
   end;
end;
If its just big blocks of text you could use the PRE tag (still need to vonvert '<' though).

Cheers,
Raymond.
well, use this code... enjoy.
brr.... browser can't insert it correctly... will send by email...
hmm... tell me your email and i'll send you...
JoeBooth, thanks for the code sample. As you demonstrate, the mechanics of the function is not complex. I merely wondered if someone has already done it, so I won't have to do it again.

rwilson, I would rather not use the PRE tag, since this means that my text will be formatted exactly as entered, and that is not acceptable.

fulGore, I would very much like to see the code you mention. My email is AndersWP@ELK.dk. I had to reject your answer in order to be able to make this comment. If your code looks good, you can just repost a dummy answer and get the points.

Regards,
AndersWP
fulgOre, you have some points coming to you if you post your answer.

AndersWP
ASKER CERTIFIED SOLUTION
Avatar of fulg0re
fulg0re

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help.

Regards,
AndersWP