Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am 99 % sure that such a function does not exist in the RTL (as of Delphi 2009). Of course - however - it is trivial to write such a function.</p> <p><strong><em>Update</em></strong></p> <p>HTTPUtil.HTMLEscape is what you are looking for:</p> <pre><code>function HTMLEscape(const Str: string): string; </code></pre> <p>I don't dare to publish the code here (copyright violation, probably), but the routine is very simple. It encodes "&lt;", ">", "&amp;", and """ to <code>&amp;lt;</code>, <code>&amp;gt;</code>, <code>&amp;amp;</code>, and <code>&amp;quot;</code>. It also replaces characters #92, #160..#255 to decimal codes, e.g. <code>&amp;#92;</code>.</p> <p>This latter step is unnecessary if the file is UTF-8, and also illogical, because higher special characters, such as ∮ are left as they are, while lower special characters, such as ×, are encoded.</p> <p><strong><em>Update 2</em></strong></p> <p>In response to the answer by Stijn Sanders, I made a simple performance test.</p> <pre><code>program Project1; {$APPTYPE CONSOLE} uses Windows, SysUtils; var t1, t2, t3, t4: Int64; i: Integer; str: string; const N = 100000; function HTMLEncode(const Data: string): string; var i: Integer; begin result := ''; for i := 1 to length(Data) do case Data[i] of '&lt;': result := result + '&amp;lt;'; '&gt;': result := result + '&amp;gt;'; '&amp;': result := result + '&amp;amp;'; '"': result := result + '&amp;quot;'; else result := result + Data[i]; end; end; function HTMLEncode2(Data: string):string; begin Result:= StringReplace( StringReplace( StringReplace( StringReplace( Data, '&amp;','&amp;amp;',[rfReplaceAll]), '&lt;','&amp;lt;',[rfReplaceAll]), '&gt;','&amp;gt;',[rfReplaceAll]), '"','&amp;quot;',[rfReplaceAll]); end; begin QueryPerformanceCounter(t1); for i := 0 to N - 1 do str := HTMLEncode('Testing. Is 3*4&lt;3+4? Do you like "A &amp; B"'); QueryPerformanceCounter(t2); QueryPerformanceCounter(t3); for i := 0 to N - 1 do str := HTMLEncode2('Testing. Is 3*4&lt;3+4? Do you like "A &amp; B"'); QueryPerformanceCounter(t4); Writeln(IntToStr(t2-t1)); Writeln(IntToStr(t4-t3)); Readln; end. </code></pre> <p>The output is</p> <pre><code>532031 801969 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload