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
LVL 1
AndersWPAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
listening
0
mhervaisCommented:
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
0
AndersWPAuthor Commented:
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
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

JoeBoothCommented:
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;
0
rwilson032697Commented:
If its just big blocks of text you could use the PRE tag (still need to vonvert '<' though).

Cheers,
Raymond.
0
fulg0reCommented:
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...
0
AndersWPAuthor Commented:
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
0
AndersWPAuthor Commented:
fulgOre, you have some points coming to you if you post your answer.

AndersWP
0
fulg0reCommented:
hi there anders!
i've been away for a while...
10x for the points (seems like u cared buot them more than i did :)

btw. what's up with your developement?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndersWPAuthor Commented:
Thanks for the help.

Regards,
AndersWP
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.