Delphi 最好的 HTML 编码器?
使用 HTTPapp.HTMLEncode( string ): String; 时我的数据似乎已损坏
HTMLEncode( 'Jo&hn D<oe' ); // returns 'Jo&am'
这是不正确的,并且正在损坏我的数据。有人对工作得更好的 VCL 组件有什么建议吗?除了花时间对所有情况进行编码
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
更新
在了解了更多关于 HTML 的知识后,我发现不需要对链接中引用的其他字符进行编码。您只需要知道四个 HTML 保留字符是
&,<,>,"
VCL HTTPApp.HTMLEncode( ) 函数的问题是由于缓冲区大小和默认 Unicode 字符串类型的新 Delphi 2009/2010 规范造成的,这可以通过以下方式解决@mason 下面说的,或者可以通过调用 WideFormatBuf( ) 而不是当前正在使用的 FormatBuf( ) 来修复它。
Seems like my data is getting corrupted when using HTTPapp.HTMLEncode( string ): String;
HTMLEncode( 'Jo&hn D<oe' ); // returns 'Jo&am'
This is not correct, and is corrupting my data. Does anyone have suggestions for VCL components that work better? Other than spending my time encoding all the cases
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
Update
After understanding more about HTML, I have found there is no need to encode the other characters referenced in my link. You would only need to know about the four HTML reserved characters being
&,<,>,"
The issue with the VCL HTTPApp.HTMLEncode( ) function is because of the buffer size and the new Delphi 2009/2010 specifications for default Unicode string types, this can be fixed the way that @mason says below, or it can be fixed with a call to WideFormatBuf( ) instead of the FormatBuf( ) that is currently in use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
替换字符串中的 <、>、& 和 " 字符非常简单。因此,您可以轻松地为此编写自己的例程。(如果您的 HTML 页面是 UTF-8,则绝对没有理由对任何字符进行编码)其他字符,例如 U+222B(积分符号)。)
但是如果您想坚持使用 Delphi RTL,那么您可以查看 HTTPUtil.HTMLEscape ,其签名与 HTTPApp.HTMLEncode 完全相同
。看看这个问题。
Replacing the <, >, &, and " characters in a string is trivial. You could thus easily write your own routine for this. (And if your HTML page is UTF-8, there is absolutely no reason to encode any other characters, such as U+222B (the integral sign).)
But if you wish to stick to the Delphi RTL, then you can have a look at HTTPUtil.HTMLEscape with the exactly same signature as HTTPApp.HTMLEncode.
Or, have a look at this SO question.
您可能使用的是 Delphi 2009 或 2010。在我看来,他们忘记更新 Unicode 的 HTMLEncode。它将错误的缓冲区长度传递给 FormatBuf。
除此之外,HTMLEncode 例程基本上是正确的,而且非常短。您可能可以自己制作副本。在每次调用 FormatBuf 的地方,它都会给出 5 个参数。第二个和第四个是整数值。每次调用时将它们加倍(只有四个),然后它就会起作用。
另外,您应该为此打开一份 QC 报告,以便将其修复。
You're probably using Delphi 2009 or 2010. It looks to me like they forgot to update HTMLEncode for Unicode. It's passing the wrong buffer lengths to FormatBuf.
The HTMLEncode routine is basically right, aside from that, and it's pretty short. You could probably just make your own copy. Everywhere it calls FormatBuf, it gives 5 parameters. The second and fourth are integer values. Double both of them in each call, (there are only four of them), and then it will work.
Also, you ought to open a QC report on this so it will get fixed.
小提示:不要将单引号 (') 转换为
'
- 某些浏览器无法理解此代码,因为'
不是有效的 HTML详情请参阅:“
的诅咒'
”和“XHTML 和 '"(提到的两个 Delphi 单元都不转换单引号)。
Small hint: do not convert single quote (') to
'
- some browsers do not understand this code because'
is not valid HTMLFor details, see: "The Curse of
'
" and "XHTML and '"(Both Delphi units mentioned do not convert single quotes).