以下函数可以用于过滤字符里的HTML代码:
function FilterHTMLCode(HTML: string; AllowURL: Boolean): string;
var
UrlStart, UrlEnd: Integer;
TempStr: string;
begin
HTML := AnsiReplaceStr(HTML, '&', '&');
HTML := AnsiReplaceStr(HTML, '
HTML := AnsiReplaceStr(HTML, '>', '>');
HTML := AnsiReplaceStr(HTML, #13, '
');
HTML := AnsiReplaceStr(HTML, #32, ' ');
if AllowURL then
begin
TempStr := '';
UrlStart := AnsiPos('http://', LowerCase(HTML));
while UrlStart > 0 do
begin
TempStr := TempStr + Copy(HTML, 1, UrlStart - 1);
HTML := Copy(HTML, UrlStart, Length(HTML));
UrlEnd := AnsiPos(' ', HTML);
if UrlEnd <= 0 then
UrlEnd := Length(HTML) + 1;
TempStr := TempStr + '' + Copy(HTML, 1, UrlEnd - 1)
+ '';
HTML := Copy(HTML, UrlEnd, Length(HTML));
UrlStart := AnsiPos('http://', LowerCase(HTML));
end;
TempStr := TempStr + Copy(HTML, 1, Length(HTML));
Result := TempStr;
end
else
begin
Result := HTML;
end;
end;