Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to convert extended characters into their HTML character entities Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Object Pascal-Strings
Language
Delphi 7.x
Views
139
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I just need a routine that scans an ordinary string and the replaces all 
occurrences of '<', '&' and all other illegal characters by the correct HTML symbol.

Answer:

In Delphi 7 (HTTPApp.pas) you have:

1   function HTMLEncode(const AStr: string): string;
2   const
3     Convert = ['&', '<', '>', '"'];
4   var
5     Sp, Rp: PChar;
6   begin
7     SetLength(Result, Length(AStr) * 10);
8     Sp := PChar(AStr);
9     Rp := PChar(Result);
10    while Sp^ <> #0 do
11    begin
12      case Sp^ of
13        '&':
14          begin
15            FormatBuf(Rp^, 5, '&', 5, []);
16            Inc(Rp, 4);
17          end;
18        '<', '>':
19          begin
20            if Sp^ = '<' then
21              FormatBuf(Rp^, 4, '<', 4, [])
22            else
23              FormatBuf(Rp^, 4, '>', 4, []);
24            Inc(Rp, 3);
25          end;
26        '"':
27          begin
28            FormatBuf(Rp^, 6, '"', 6, []);
29            Inc(Rp, 5);
30          end;
31      else
32        Rp^ := Sp^
33      end;
34      Inc(Rp);
35      Inc(Sp);
36    end;
37    SetLength(Result, Rp - PChar(Result));
38  end;


which is pretty good. It will use quite a bit of memory on long input strings, 
though. For some reason it sets the result buffer to 10 times the length of the 
input buffer when 6 times would have been enough (for the worst case, all quote 
chars (").

I rolled my own before D7 was released (part of my WOS framework - found on the D7 
Companion CD):

39  function HTMLEncode(const S: string): string;
40  const
41    ConversionSet = ['&', '<', '>', '"', '+']; {The '+' is because of a IE bug}
42    ConversionChars: PChar = '&<>"+';
43    Entities: array[1..5] of string = ('&', '<', '>', '"', '+');
44  var
45    Sp, Rp: PChar;
46    P: integer;
47  begin
48    SetLength(Result, Length(S) * 6); {Ouch... ( worst case is all "'s )}
49    Sp := PChar(S);
50    Rp := PChar(Result);
51    while Sp^ <> #0 do
52    begin
53      if not (Sp^ in ConversionSet) then
54      begin
55        Rp^ := Sp^;
56        Inc(Rp);
57      end
58      else
59      begin
60        P := StrScan(ConversionChars, Sp^) - ConversionChars + 1;
61        StrCopy(RP, PChar(Entities[P]));
62        Inc(Rp, Length(Entities[P]));
63      end;
64      Inc(Sp);
65    end;
66    SetLength(Result, Rp - PChar(Result));
67  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC