是否有Delphi标准函数用于转义HTML?

编程入门 行业动态 更新时间:2024-10-11 01:17:57
本文介绍了是否有Delphi标准函数用于转义HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个报告,应该采取网格控制并生成HTML输出。网格中的一列可以显示多个值,或< Any> 。当它输出到HTML时,当然最终会变为空白。

我可能会写一些例程来使用StringReplace将其转换为 & lt; Any& gt; ,所以它会正确显示这个特定的情况,但是我认为RTL中可能有一个已经被测试,并且是正确的。任何人都知道我在哪里可以找到它?

解决方案

我99%肯定这样的功能不存在于RTL的德尔福2009)。然而,当然,写这样一个功能是微不足道的。

更新

HTTPUtil.HTMLEscape是你正在寻找的:

function HTMLEscape(const Str:string ):string;

我不敢在这里发布代码(侵犯版权,大概),但是例程是很简单。它将<,>,&和编码到& lt; ,& / code>,& amp; amp; 和& quot; ,还可以替换字符#92 ,#160 ..#255到十进制代码,例如&#92; 。

后一步是如果文件是UTF-8,则不必要,也是不合逻辑的,因为较高的特殊字符(如∮)保持原样,而较低的特殊字符(如×)被编码。

更新2

为了回应Stijn Sanders的回答,我做了一个简单的性能测试。

程序Project1; {$ APPTYPE CONSOLE} 使用 Windows,SysUtils; var t1,t2,t3,t4:Int64; i:整数; str:string; const N = 100000; 函数HTMLEncode(const Data:string):string; var i:Integer; begin result:=''; for i:= 1 to length(Data )do case '<':result:= result +'& lt;';的数据[i] '>':result:= result +'& gt;'; '&':result:= result +'& amp;'; ':result:= result +'& quot;'; else result:= result + Data [i]; end; end; function HTMLEncode2(Data:string):string; begin 结果:= StringReplace( StringReplace( StringReplace( StringReplace( Data,'&','& amp;',[rfReplaceAll]),'<','& ',[rfReplaceAll]),'>','& gt;',[rfReplaceAll]),'','& quot;',[rfReplaceAll]); 结束 begin QueryPerformanceCounter(t1); for i:= 0 to N - 1 do str:= HTMLEncode('Testing。Is 3 * 4< 3 + 4?你喜欢A& B QueryPerformanceCounter(t2); QueryPerformanceCounter(t3); for i:= 0 to N - 1 do str:= HTMLEncode2('Testing。Is 3 * 4< 3 + 4?你喜欢A& B QueryPerformanceCounter(t4); Writeln(IntToStr(t2-t1)); Writeln(IntToStr(t4-t3)); Readln; 结束。

输出是

532031 801969

I've got a report that's supposed to take a grid control and produce HTML output. One of the columns in the grid can display any of a number of values, or <Any>. When this gets output to HTML, of course, it ends up blank.

I could probably write up some routine to use StringReplace to turn that into &lt;Any&gt; so it would display this particular case correctly, but I figure there's probably one in the RTL somewhere that's already been tested and does it right. Anyone know where I could find it?

解决方案

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.

Update

HTTPUtil.HTMLEscape is what you are looking for:

function HTMLEscape(const Str: string): string;

I don't dare to publish the code here (copyright violation, probably), but the routine is very simple. It encodes "<", ">", "&", and """ to &lt;, &gt;, &amp;, and &quot;. It also replaces characters #92, #160..#255 to decimal codes, e.g. &#92;.

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.

Update 2

In response to the answer by Stijn Sanders, I made a simple performance test.

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 + '&lt;'; '>': result := result + '&gt;'; '&': result := result + '&amp;'; '"': result := result + '&quot;'; else result := result + Data[i]; end; end; function HTMLEncode2(Data: string):string; begin Result:= StringReplace( StringReplace( StringReplace( StringReplace( Data, '&','&amp;',[rfReplaceAll]), '<','&lt;',[rfReplaceAll]), '>','&gt;',[rfReplaceAll]), '"','&quot;',[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.

The output is

532031 801969

更多推荐

是否有Delphi标准函数用于转义HTML?

本文发布于:2023-11-22 01:38:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   标准   Delphi   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!