如何可靠地转换特殊字符,例如 £使用 Cocoa 转换成 HTML 等价物?
我正在尝试向 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:@"<"];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@">"];
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本课程应该对您有帮助:
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
我认为您的主要问题是您没有将 HTML 页面编码并声明为 UTF-8。虽然您提到的一些实体是一个真正的问题,需要转换,例如
>
到>
(@Joel Martinez 链接的代码将有所帮助那里),像£
符号这样的东西就可以正常工作,只要页面被声明并编码为 unicode 格式,例如 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" />