HTML实体编码(在object-c上将iPhone转换为'<'到'& lt;') [英] HTML entity encoding (convert '<' to '<') on iPhone in objective-c

查看:146
本文介绍了HTML实体编码(在object-c上将iPhone转换为'<'到'& lt;')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款具有inApp-mail发送功能的iPhone应用程序。到目前为止一切都那么好,但现在我想避免html注入,因为邮件的某些部分是用户生成的文本。

I'm developing an application for the iPhone that has inApp-mail sending capabilities. So far so good, but now I want to avoid html-injections as some parts of the mail are user-generated texts.

基本上我搜索这样的东西:

Basically I search for something like this:

// inits
NSString *sourceString = [NSString stringWithString:@"Hello world! Grüße dich Welt <-- This is in German."];

//                                          -----   THAT'S WHAT I'M LOOKING FOR
// pseudo-code                              |
//                                          V
NSString *htmlEncodedString = [sourceString htmlEncode];

// log
NSLog(@"source string: %@", sourceString);
NSLog(@"encoded string: %@", htmlEncodedString);

预期输出

源字符串:Hello world! GrüßedichWelt< - 这是德语。

编码字符串:Hello world! Gr&#252;&#223; e dich Welt& lt; - 这是德语。




I已经谷歌搜索并浏览了几个SO的问题和答案,但所有这些都与 URL编码有关,而这不是我真正需要的(我试过 stringByAddingPercentEscapesUsingEncoding 没有运气 - 它创造了'ü'应该是ü)的%C3%BC。

Expected output
source string: Hello world! Grüße dich Welt <-- This is in German.
encoded string: Hello world! Gr&#252;&#223;e dich Welt &lt;-- This is in German.

I already googled and looked through several of SO's questions and answers, but all of them seem to be related to URL-encoding and that's not what I really need (I tried stringByAddingPercentEscapesUsingEncoding with no luck - it creates %C3%BC out of an 'ü' that should be an ü).

代码示例真的很棒(纠正我的?)...

A code sample would be really great (correcting mine?)...

-

在此先感谢,

Markus

--
Thanks in advance,
Markus

推荐答案

查看我的 HTML的NSString类别。以下是可用的方法:

Check out my NSString category for HTML. Here are the methods available:

- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;

这篇关于HTML实体编码(在object-c上将iPhone转换为'&lt;'到'&amp; lt;')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆