如何可靠地转换特殊字符,例如 £使用 Cocoa 转换成 HTML 等价物?

发布于 2024-12-02 06:28:39 字数 1683 浏览 2 评论 0原文

我正在尝试向 Mail.app 提供一些简单的 html:列表、粗体字体、一些斜体。但是,我注意到,如果我使用像 £ 这样的字符,那么 Mail.app 就不会显示任何内容。我意识到我需要转换为 HTML 实体,例如 £ (此处的完整列表:http://www.w3schools.com/tags/ref_entities.asp)。我有一个适用于我的用户提出的大多数字符的部分解决方案,但它远不是一个可靠的解决方案:

- (NSString*) makeValidHTML:(NSString*)str {
  str = [str stringByReplacingOccurrencesOfString:@"£" withString:@"£"];
  str = [str stringByReplacingOccurrencesOfString:@"¢" withString:@"¢"];
  str = [str stringByReplacingOccurrencesOfString:@"¥" withString:@"¥"];
  str = [str stringByReplacingOccurrencesOfString:@"©" withString:@"©"];
  str = [str stringByReplacingOccurrencesOfString:@"®" withString:@"®"];
  str = [str stringByReplacingOccurrencesOfString:@"°" withString:@"°"];
  str = [str stringByReplacingOccurrencesOfString:@"¿" withString:@"¿"];
  str = [str stringByReplacingOccurrencesOfString:@"¡" withString:@"¡"];
  str = [str stringByReplacingOccurrencesOfString:@"‘" withString:@"'"];
  str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
  str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
  str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"""];
  str = [str stringByReplacingOccurrencesOfString:@"“" withString:@"""];
  str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
  str = [str stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
  return str;
}

是否有一种标准方法可以做到这一点,而不必列出每个可能的保留字符?

I'm trying to feed Mail.app some simple html: lists, bold font, some italics. However, I noticed that if I use characters like £, then Mail.app just doesn't show anything. I realized I need to convert to HTML entities, like £ (full list here: http://www.w3schools.com/tags/ref_entities.asp). I have a partial solution that works for most characters my users have come up with, but it's far from being a solid fix:

- (NSString*) makeValidHTML:(NSString*)str {
  str = [str stringByReplacingOccurrencesOfString:@"£" withString:@"£"];
  str = [str stringByReplacingOccurrencesOfString:@"¢" withString:@"¢"];
  str = [str stringByReplacingOccurrencesOfString:@"¥" withString:@"¥"];
  str = [str stringByReplacingOccurrencesOfString:@"©" withString:@"©"];
  str = [str stringByReplacingOccurrencesOfString:@"®" withString:@"®"];
  str = [str stringByReplacingOccurrencesOfString:@"°" withString:@"°"];
  str = [str stringByReplacingOccurrencesOfString:@"¿" withString:@"¿"];
  str = [str stringByReplacingOccurrencesOfString:@"¡" withString:@"¡"];
  str = [str stringByReplacingOccurrencesOfString:@"‘" withString:@"'"];
  str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
  str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
  str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"""];
  str = [str stringByReplacingOccurrencesOfString:@"“" withString:@"""];
  str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
  str = [str stringByReplacingOccurrencesOfString:@">" withString:@">"];
  return str;
}

Is there a standard way to do this without having to list every possible reserved character?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素染倾城色 2024-12-09 06:28:39

本课程应该对您有帮助:
https://github.com/mwaterfall/MWFeedParser/blob/ master/Classes/NSString+HTML.m

从另一个 SO 答案中检索到的链接:
转换到&在 Objective-C 中

This class should be helpful to you:
https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m

Link retrieved from this other SO answer:
Converting & to & in Objective-C

中性美 2024-12-09 06:28:39

我认为您的主要问题是您没有将 HTML 页面编码并声明为 UTF-8。虽然您提到的一些实体是一个真正的问题,需要转换,例如 >> (@Joel Martinez 链接的代码将有所帮助那里),像 £ 符号这样的东西就可以正常工作,只要页面被声明并编码为 un​​icode 格式,例如 UTF-8:

I think your main problem is that you're not encoding and declaring your HTML page as UTF-8. While some of the entities you mention are a genuine issue and need to be converted, such as > to > (the code @Joel Martinez linked to will help there), things like the £ symbol will work just fine as they are, provided the page is declared and encoded to be a unicode format such as UTF-8:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文