HTML character decoding in Objective-C / Cocoa Touch

后端 未结 13 2117
我寻月下人不归
我寻月下人不归 2020-11-22 10:24

First of all, I found this: Objective C HTML escape/unescape, but it doesn\'t work for me.

My encoded characters (come from a RSS feed, btw) look like this: &a

13条回答
  •  Happy的楠姐
    2020-11-22 11:03

    Those are called Character Entity References. When they take the form of &#; they are called numeric entity references. Basically, it's a string representation of the byte that should be substituted. In the case of &, it represents the character with the value of 38 in the ISO-8859-1 character encoding scheme, which is &.

    The reason the ampersand has to be encoded in RSS is it's a reserved special character.

    What you need to do is parse the string and replace the entities with a byte matching the value between &# and ;. I don't know of any great ways to do this in objective C, but this stack overflow question might be of some help.

    Edit: Since answering this some two years ago there are some great solutions; see @Michael Waterfall's answer below.

提交回复
热议问题