Mam 99% pewności, że taka funkcja nie istnieje w RTL (od Delphi 2009). Oczywiście - napisanie takiej funkcji jest trywialne.
Aktualizacja
HTTPUtil.HTMLEscape jest to, czego szukasz:
function HTMLEscape(const Str: string): string;
nie odważył się opublikować kod tutaj (naruszenie praw autorskich, chyba), ale rutyna jest bardzo prosty. Koduje "<", ">", "&" i ""”, aby <
, >
, &
i "
. Zastępuje ono także znaki # 92, # 160 .. # 255 na dziesiętne kody, np \
.
Ten ostatni etap jest konieczne, jeśli plik jest UTF-8, a także nielogicznego, ponieważ wyższe znaków specjalnych, takich jak ∮ pozostały jako takie, podczas gdy niższe znaki specjalne takie jak krotne są kodowane.
Aktualizacja 2
W odpowiedzi na odpowiedź Stijn Sanders wykonałem prosty test wydajności.
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.
Wyjście jest
532031
801969
Sposób Delphi na ucieczkę z HTML ... Intraweb! ;-) – LachlanG