html - 读取 Delphi 字符串中的 HTML 特殊字符
问题描述
我有一个使用 Expression Web 4 构建的网页“index.html”,其中包含一个用 id 分隔的值:
<html>
<head></head>
<body>
<... some html code ...>
<!--MYVALUEID-->
Dernières News
<... some html code ...>
</body>
</html>
使用我的 delphi 应用程序,我将页面加载到 TStringList 并将值读取到 TEDIT :
S:=TStringList.Create;
S.LoadFromFile('path\index.html');
Edit1.Text:=S[S.IndexOf('<!--MYVALUEID-->')+1];
S.Free;
问题是重音字符,因为我在 TEDIT 中得到了这个:“Dernières News”
在 Expression Web 代码中,文本是正确的:Dernières 新闻
当我在记事本中打开 index.html 时,它会显示:Dernières News
记事本中的文件显示为UTF8
当使用 HTTPApp.HTMLDecode() 我得到了:Dernières News
还有 System.NetEncoding、TNetEncoding.HTML.Decode :Dernières News
是否有可靠的例程来解码 html 特殊字符转换?
我在 SO 中检查了许多问题并尝试了上述解决方案,但没有任何反应。
提前谢谢,我卡住了。
解决方案
由于您的 HTML 文件以 UTF-8 编码,因此您应该在调用时指定它LoadFromFile()
:
S := TStringList.Create;
S.LoadFromFile('path\index.html', TEncoding.UTF8);
否则使用 ANSI 编码。
推荐阅读
- flutter - showMaterialBanner 不会隐藏在更改路线上查找已停用小部件的祖先是不安全的
- typescript - 工厂函数的通用类型
- json - 如何计算表格中所有列单元格的总和?
- php - 如何在 php 中使用 phpmailer 将原始 html 代码正确发送到 gmail
- java - java manytomany映射不创建
- django - django 不按类别名称过滤
- python - 我们如何用python打印浏览器的http流?[喜欢浏览器网络标签]
- r - Emmeans 没有从模型中给我正确的调整方法
- flutter - 为什么sizer将填充值作为高度处理?
- tensorflow - 训练具有多个输出的自动编码器期间的 NaN 损失值