Sono sicuro al 99% che tale funzione non esiste nella RTL (come da Delphi 2009). Naturalmente - tuttavia - è banale scrivere una tale funzione.
Aggiornamento
HTTPUtil.HTMLEscape è quello che state cercando:
function HTMLEscape(const Str: string): string;
non oso di pubblicare il codice qui (violazione di copyright, probabilmente), ma la routine è molto semplice Codifica "<", ">", "&", e """ per <
, >
, &
e "
. Essa sostituisce anche caratteri # 92, # 160 .. # 255 in decimale codici, ad esempio \
.
quest'ultimo passaggio non è necessario se il file è UTF-8, e anche illogica, perché i caratteri speciali più elevate, come ad esempio ∮ sono lasciati così come sono, mentre i caratteri speciali inferiori, come ×, sono codificati.
Aggiornamento 2
In risposta alla risposta di Stijn Sanders, ho fatto un semplice test delle prestazioni.
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
'<': result := result + '<';
'>': result := result + '>';
'&': result := result + '&';
'"': result := result + '"';
else
result := result + Data[i];
end;
end;
function HTMLEncode2(Data: string):string;
begin
Result:=
StringReplace(
StringReplace(
StringReplace(
StringReplace(
Data,
'&','&',[rfReplaceAll]),
'<','<',[rfReplaceAll]),
'>','>',[rfReplaceAll]),
'"','"',[rfReplaceAll]);
end;
begin
QueryPerformanceCounter(t1);
for i := 0 to N - 1 do
str := HTMLEncode('Testing. Is 3*4<3+4? Do you like "A & B"');
QueryPerformanceCounter(t2);
QueryPerformanceCounter(t3);
for i := 0 to N - 1 do
str := HTMLEncode2('Testing. Is 3*4<3+4? Do you like "A & B"');
QueryPerformanceCounter(t4);
Writeln(IntToStr(t2-t1));
Writeln(IntToStr(t4-t3));
Readln;
end.
L'uscita è
532031
801969
Un modo Delphi per sfuggire all'HTML ... Intraweb! ;-) – LachlanG