[英]HTML character decoding in Objective-C / Cocoa Touch
First of all, I found this: 首先,我发现了这一点: Objective C HTML escape/unescape , but it doesn't work for me.
目标C HTML转义/转义 ,但对我不起作用。
My encoded characters (come from a RSS feed, btw) look like this: &
我的编码字符(来自RSS feed,顺便说一句)如下:
&
I searched all over the net and found related discussions, but no fix for my particular encoding, I think they are called hexadecimal characters. 我在网上搜索了所有内容,并找到了相关的讨论,但是没有解决我的特定编码问题,我认为它们被称为十六进制字符。
Check out my NSString category for HTML . 查看我的HTML的NSString类别 。 Here are the methods available:
以下是可用的方法:
- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
The one by Daniel is basically very nice, and I fixed a few issues there: 丹尼尔(Daniel)撰写的文章基本上非常好,我在那里修复了一些问题:
removed the skipping character for NSSCanner (otherwise spaces between two continuous entities would be ignored 删除了NSSCanner的跳过字符(否则两个连续实体之间的空格将被忽略
[scanner setCharactersToBeSkipped:nil]; [scanner setCharactersToBeSkipped:nil];
fixed the parsing when there are isolated '&' symbols (I am not sure what is the 'correct' output for this, I just compared it against firefox): 修复了当存在孤立的“&”符号时的解析(我不确定对此的“正确”输出是什么,我只是将它与firefox进行了比较):
eg 例如
&#ABC DF & B' & C' Items (288)
here is the modified code: 这是修改后的代码:
- (NSString *)stringByDecodingXMLEntities {
NSUInteger myLength = [self length];
NSUInteger ampIndex = [self rangeOfString:@"&" options:NSLiteralSearch].location;
// Short-circuit if there are no ampersands.
if (ampIndex == NSNotFound) {
return self;
}
// Make result string with some extra capacity.
NSMutableString *result = [NSMutableString stringWithCapacity:(myLength * 1.25)];
// First iteration doesn't need to scan to & since we did that already, but for code simplicity's sake we'll do it again with the scanner.
NSScanner *scanner = [NSScanner scannerWithString:self];
[scanner setCharactersToBeSkipped:nil];
NSCharacterSet *boundaryCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@" \t\n\r;"];
do {
// Scan up to the next entity or the end of the string.
NSString *nonEntityString;
if ([scanner scanUpToString:@"&" intoString:&nonEntityString]) {
[result appendString:nonEntityString];
}
if ([scanner isAtEnd]) {
goto finish;
}
// Scan either a HTML or numeric character entity reference.
if ([scanner scanString:@"&" intoString:NULL])
[result appendString:@"&"];
else if ([scanner scanString:@"'" intoString:NULL])
[result appendString:@"'"];
else if ([scanner scanString:@""" intoString:NULL])
[result appendString:@"\""];
else if ([scanner scanString:@"<" intoString:NULL])
[result appendString:@"<"];
else if ([scanner scanString:@">" intoString:NULL])
[result appendString:@">"];
else if ([scanner scanString:@"&#" intoString:NULL]) {
BOOL gotNumber;
unsigned charCode;
NSString *xForHex = @"";
// Is it hex or decimal?
if ([scanner scanString:@"x" intoString:&xForHex]) {
gotNumber = [scanner scanHexInt:&charCode];
}
else {
gotNumber = [scanner scanInt:(int*)&charCode];
}
if (gotNumber) {
[result appendFormat:@"%C", (unichar)charCode];
[scanner scanString:@";" intoString:NULL];
}
else {
NSString *unknownEntity = @"";
[scanner scanUpToCharactersFromSet:boundaryCharacterSet intoString:&unknownEntity];
[result appendFormat:@"&#%@%@", xForHex, unknownEntity];
//[scanner scanUpToString:@";" intoString:&unknownEntity];
//[result appendFormat:@"&#%@%@;", xForHex, unknownEntity];
NSLog(@"Expected numeric character entity but got &#%@%@;", xForHex, unknownEntity);
}
}
else {
NSString *amp;
[scanner scanString:@"&" intoString:&]; //an isolated & symbol
[result appendString:amp];
/*
NSString *unknownEntity = @"";
[scanner scanUpToString:@";" intoString:&unknownEntity];
NSString *semicolon = @"";
[scanner scanString:@";" intoString:&semicolon];
[result appendFormat:@"%@%@", unknownEntity, semicolon];
NSLog(@"Unsupported XML character entity %@%@", unknownEntity, semicolon);
*/
}
}
while (![scanner isAtEnd]);
finish:
return result;
}
Those are called Character Entity References . 这些被称为字符实体引用 。 When they take the form of
&#<number>;
当它们采用
&#<number>;
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 &
. ,它表示ISO-8859-1字符编码方案中值为38的字符
&
。
The reason the ampersand has to be encoded in RSS is it's a reserved special character. 必须在RSS中对与号进行编码的原因是它是保留的特殊字符。
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.
我不知道在目标C中执行此操作的任何好方法,但是此堆栈溢出问题可能会有所帮助。
Edit: Since answering this some two years ago there are some great solutions; 编辑:自从两年前回答这个问题以来,有一些很好的解决方案; see @Michael Waterfall's answer below.
请参阅下面的@Michael Waterfall答案。
As of iOS 7, you can decode HTML characters natively by using an NSAttributedString
with the NSHTMLTextDocumentType
attribute: 由于iOS的7,你可以通过使用本地解码HTML字符
NSAttributedString
与NSHTMLTextDocumentType
属性:
NSString *htmlString = @" & & < > ™ © ♥ ♣ ♠ ♦";
NSData *stringData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
NSAttributedString *decodedString;
decodedString = [[NSAttributedString alloc] initWithData:stringData
options:options
documentAttributes:NULL
error:NULL];
The decoded attributed string will now be displayed as: & & < > ™ © ♥ ♣ ♠ ♦. 解码后的属性字符串现在将显示为:&&<>™©♥♣♦。
Note: This will only work if called on the main thread. 注意:仅在主线程上调用时,此方法才有效。
Nobody seems to mention one of the simplest options: Google Toolbox for Mac 似乎没有人提到最简单的选项之一: Mac版Google工具箱
(Despite the name, this works on iOS too.) (尽管名称,它也适用于iOS。)
https://github.com/google/google-toolbox-for-mac/blob/master/Foundation/GTMNSString%2BHTML.h https://github.com/google/google-toolbox-for-mac/blob/master/Foundation/GTMNSString%2BHTML.h
/// Get a string where internal characters that are escaped for HTML are unescaped
//
/// For example, '&' becomes '&'
/// Handles   and 2 cases as well
///
// Returns:
// Autoreleased NSString
//
- (NSString *)gtm_stringByUnescapingFromHTML;
And I had to include only three files in the project: header, implementation and GTMDefines.h
. 我只需要在项目中包括三个文件:标头,实现和
GTMDefines.h
。
I ought to post this on GitHub or something. 我应该将其发布在GitHub之类的东西上。 This goes in a category of NSString, uses
NSScanner
for the implementation, and handles both hex and decimal numeric character entities as well as the usual symbolic ones. 这属于NSString类别,使用
NSScanner
进行实现,并处理十六进制和十进制数字字符实体以及通常的符号实体。
Also, it handles malformed strings (when you have an & followed by an invalid sequence of characters) relatively gracefully, which turned out to be crucial in my released app that uses this code. 而且,它可以相对优雅地处理格式错误的字符串(当您带有&后跟无效的字符序列时),这在使用此代码的我发布的应用程序中至关重要。
- (NSString *)stringByDecodingXMLEntities {
NSUInteger myLength = [self length];
NSUInteger ampIndex = [self rangeOfString:@"&" options:NSLiteralSearch].location;
// Short-circuit if there are no ampersands.
if (ampIndex == NSNotFound) {
return self;
}
// Make result string with some extra capacity.
NSMutableString *result = [NSMutableString stringWithCapacity:(myLength * 1.25)];
// First iteration doesn't need to scan to & since we did that already, but for code simplicity's sake we'll do it again with the scanner.
NSScanner *scanner = [NSScanner scannerWithString:self];
do {
// Scan up to the next entity or the end of the string.
NSString *nonEntityString;
if ([scanner scanUpToString:@"&" intoString:&nonEntityString]) {
[result appendString:nonEntityString];
}
if ([scanner isAtEnd]) {
goto finish;
}
// Scan either a HTML or numeric character entity reference.
if ([scanner scanString:@"&" intoString:NULL])
[result appendString:@"&"];
else if ([scanner scanString:@"'" intoString:NULL])
[result appendString:@"'"];
else if ([scanner scanString:@""" intoString:NULL])
[result appendString:@"\""];
else if ([scanner scanString:@"<" intoString:NULL])
[result appendString:@"<"];
else if ([scanner scanString:@">" intoString:NULL])
[result appendString:@">"];
else if ([scanner scanString:@"&#" intoString:NULL]) {
BOOL gotNumber;
unsigned charCode;
NSString *xForHex = @"";
// Is it hex or decimal?
if ([scanner scanString:@"x" intoString:&xForHex]) {
gotNumber = [scanner scanHexInt:&charCode];
}
else {
gotNumber = [scanner scanInt:(int*)&charCode];
}
if (gotNumber) {
[result appendFormat:@"%C", charCode];
}
else {
NSString *unknownEntity = @"";
[scanner scanUpToString:@";" intoString:&unknownEntity];
[result appendFormat:@"&#%@%@;", xForHex, unknownEntity];
NSLog(@"Expected numeric character entity but got &#%@%@;", xForHex, unknownEntity);
}
[scanner scanString:@";" intoString:NULL];
}
else {
NSString *unknownEntity = @"";
[scanner scanUpToString:@";" intoString:&unknownEntity];
NSString *semicolon = @"";
[scanner scanString:@";" intoString:&semicolon];
[result appendFormat:@"%@%@", unknownEntity, semicolon];
NSLog(@"Unsupported XML character entity %@%@", unknownEntity, semicolon);
}
}
while (![scanner isAtEnd]);
finish:
return result;
}
This is the way I do it using RegexKitLite framework: 这是我使用RegexKitLite框架的方法:
-(NSString*) decodeHtmlUnicodeCharacters: (NSString*) html {
NSString* result = [html copy];
NSArray* matches = [result arrayOfCaptureComponentsMatchedByRegex: @"\\&#([\\d]+);"];
if (![matches count])
return result;
for (int i=0; i<[matches count]; i++) {
NSArray* array = [matches objectAtIndex: i];
NSString* charCode = [array objectAtIndex: 1];
int code = [charCode intValue];
NSString* character = [NSString stringWithFormat:@"%C", code];
result = [result stringByReplacingOccurrencesOfString: [array objectAtIndex: 0]
withString: character];
}
return result;
} }
Hope this will help someone. 希望这会帮助某人。
you can use just this function to solve this problem. 您可以仅使用此功能来解决此问题。
+ (NSString*) decodeHtmlUnicodeCharactersToString:(NSString*)str
{
NSMutableString* string = [[NSMutableString alloc] initWithString:str]; // #&39; replace with '
NSString* unicodeStr = nil;
NSString* replaceStr = nil;
int counter = -1;
for(int i = 0; i < [string length]; ++i)
{
unichar char1 = [string characterAtIndex:i];
for (int k = i + 1; k < [string length] - 1; ++k)
{
unichar char2 = [string characterAtIndex:k];
if (char1 == '&' && char2 == '#' )
{
++counter;
unicodeStr = [string substringWithRange:NSMakeRange(i + 2 , 2)];
// read integer value i.e, 39
replaceStr = [string substringWithRange:NSMakeRange (i, 5)]; // #&39;
[string replaceCharactersInRange: [string rangeOfString:replaceStr] withString:[NSString stringWithFormat:@"%c",[unicodeStr intValue]]];
break;
}
}
}
[string autorelease];
if (counter > 1)
return [self decodeHtmlUnicodeCharactersToString:string];
else
return string;
}
Here's a Swift version of Walty Yeung's answer : 这是杨永Wal的答案的Swift版本:
extension String {
static private let mappings = [""" : "\"","&" : "&", "<" : "<", ">" : ">"," " : " ","¡" : "¡","¢" : "¢","£" : " £","¤" : "¤","¥" : "¥","¦" : "¦","§" : "§","¨" : "¨","©" : "©","ª" : " ª","«" : "«","¬" : "¬","®" : "®","¯" : "¯","°" : "°","±" : "±","² " : "²","³" : "³","´" : "´","µ" : "µ","¶" : "¶","·" : "·","¸" : "¸","¹" : "¹","º" : "º","»" : "»&","frac14" : "¼","½" : "½","¾" : "¾","¿" : "¿","×" : "×","÷" : "÷","Ð" : "Ð","ð" : "ð","Þ" : "Þ","þ" : "þ","Æ" : "Æ","æ" : "æ","&OElig" : "Œ","&oelig" : "œ","Å" : "Å","Ø" : "Ø","Ç" : "Ç","ç" : "ç","ß" : "ß","Ñ" : "Ñ","ñ":"ñ",]
func stringByDecodingXMLEntities() -> String {
guard let _ = self.rangeOfString("&", options: [.LiteralSearch]) else {
return self
}
var result = ""
let scanner = NSScanner(string: self)
scanner.charactersToBeSkipped = nil
let boundaryCharacterSet = NSCharacterSet(charactersInString: " \t\n\r;")
repeat {
var nonEntityString: NSString? = nil
if scanner.scanUpToString("&", intoString: &nonEntityString) {
if let s = nonEntityString as? String {
result.appendContentsOf(s)
}
}
if scanner.atEnd {
break
}
var didBreak = false
for (k,v) in String.mappings {
if scanner.scanString(k, intoString: nil) {
result.appendContentsOf(v)
didBreak = true
break
}
}
if !didBreak {
if scanner.scanString("&#", intoString: nil) {
var gotNumber = false
var charCodeUInt: UInt32 = 0
var charCodeInt: Int32 = -1
var xForHex: NSString? = nil
if scanner.scanString("x", intoString: &xForHex) {
gotNumber = scanner.scanHexInt(&charCodeUInt)
}
else {
gotNumber = scanner.scanInt(&charCodeInt)
}
if gotNumber {
let newChar = String(format: "%C", (charCodeInt > -1) ? charCodeInt : charCodeUInt)
result.appendContentsOf(newChar)
scanner.scanString(";", intoString: nil)
}
else {
var unknownEntity: NSString? = nil
scanner.scanUpToCharactersFromSet(boundaryCharacterSet, intoString: &unknownEntity)
let h = xForHex ?? ""
let u = unknownEntity ?? ""
result.appendContentsOf("&#\(h)\(u)")
}
}
else {
scanner.scanString("&", intoString: nil)
result.appendContentsOf("&")
}
}
} while (!scanner.atEnd)
return result
}
}
Actually the great MWFeedParser framework of Michael Waterfall (referred to his answer) has been forked by rmchaara who has update it with ARC support! 实际上,迈克尔瀑布很棒的MWFeedParser框架(请参阅他的回答)是由rmchaara派生的,他通过ARC支持对其进行了更新!
You can find it in Github here 您可以在Github上找到它在这里
It really works great, I used stringByDecodingHTMLEntities method and works flawlessly. 它确实很棒,我使用了stringByDecodingHTMLEntities方法并且完美无缺。
As if you need another solution! 好像您需要其他解决方案! This one is pretty simple and quite effective:
这很简单,也很有效:
@interface NSString (NSStringCategory)
- (NSString *) stringByReplacingISO8859Codes;
@end
@implementation NSString (NSStringCategory)
- (NSString *) stringByReplacingISO8859Codes
{
NSString *dataString = self;
do {
//*** See if string contains &# prefix
NSRange range = [dataString rangeOfString: @"&#" options: NSRegularExpressionSearch];
if (range.location == NSNotFound) {
break;
}
//*** Get the next three charaters after the prefix
NSString *isoHex = [dataString substringWithRange: NSMakeRange(range.location + 2, 3)];
//*** Create the full code for replacement
NSString *isoString = [NSString stringWithFormat: @"&#%@;", isoHex];
//*** Convert to decimal integer
unsigned decimal = 0;
NSScanner *scanner = [NSScanner scannerWithString: [NSString stringWithFormat: @"0%@", isoHex]];
[scanner scanHexInt: &decimal];
//*** Use decimal code to get unicode character
NSString *unicode = [NSString stringWithFormat:@"%C", decimal];
//*** Replace all occurences of this code in the string
dataString = [dataString stringByReplacingOccurrencesOfString: isoString withString: unicode];
} while (TRUE); //*** Loop until we hit the NSNotFound
return dataString;
}
@end
If you have the Character Entity Reference as a string, eg @"2318"
, you can extract a recoded NSString with the correct unicode character using strtoul
; 如果您将字符实体引用作为字符串,例如
@"2318"
,则可以使用strtoul
提取具有正确unicode字符的经过重新编码的NSString;
NSString *unicodePoint = @"2318"
unichar iconChar = (unichar) strtoul(unicodePoint.UTF8String, NULL, 16);
NSString *recoded = [NSString stringWithFormat:@"%C", iconChar];
NSLog(@"recoded: %@", recoded");
// prints out "recoded: ⌘"
Swift 3 version of Jugale's answer Swift 3版本的Jugale的答案
extension String {
static private let mappings = [""" : "\"","&" : "&", "<" : "<", ">" : ">"," " : " ","¡" : "¡","¢" : "¢","£" : " £","¤" : "¤","¥" : "¥","¦" : "¦","§" : "§","¨" : "¨","©" : "©","ª" : " ª","«" : "«","¬" : "¬","®" : "®","¯" : "¯","°" : "°","±" : "±","² " : "²","³" : "³","´" : "´","µ" : "µ","¶" : "¶","·" : "·","¸" : "¸","¹" : "¹","º" : "º","»" : "»&","frac14" : "¼","½" : "½","¾" : "¾","¿" : "¿","×" : "×","÷" : "÷","Ð" : "Ð","ð" : "ð","Þ" : "Þ","þ" : "þ","Æ" : "Æ","æ" : "æ","&OElig" : "Œ","&oelig" : "œ","Å" : "Å","Ø" : "Ø","Ç" : "Ç","ç" : "ç","ß" : "ß","Ñ" : "Ñ","ñ":"ñ",]
func stringByDecodingXMLEntities() -> String {
guard let _ = self.range(of: "&", options: [.literal]) else {
return self
}
var result = ""
let scanner = Scanner(string: self)
scanner.charactersToBeSkipped = nil
let boundaryCharacterSet = CharacterSet(charactersIn: " \t\n\r;")
repeat {
var nonEntityString: NSString? = nil
if scanner.scanUpTo("&", into: &nonEntityString) {
if let s = nonEntityString as? String {
result.append(s)
}
}
if scanner.isAtEnd {
break
}
var didBreak = false
for (k,v) in String.mappings {
if scanner.scanString(k, into: nil) {
result.append(v)
didBreak = true
break
}
}
if !didBreak {
if scanner.scanString("&#", into: nil) {
var gotNumber = false
var charCodeUInt: UInt32 = 0
var charCodeInt: Int32 = -1
var xForHex: NSString? = nil
if scanner.scanString("x", into: &xForHex) {
gotNumber = scanner.scanHexInt32(&charCodeUInt)
}
else {
gotNumber = scanner.scanInt32(&charCodeInt)
}
if gotNumber {
let newChar = String(format: "%C", (charCodeInt > -1) ? charCodeInt : charCodeUInt)
result.append(newChar)
scanner.scanString(";", into: nil)
}
else {
var unknownEntity: NSString? = nil
scanner.scanUpToCharacters(from: boundaryCharacterSet, into: &unknownEntity)
let h = xForHex ?? ""
let u = unknownEntity ?? ""
result.append("&#\(h)\(u)")
}
}
else {
scanner.scanString("&", into: nil)
result.append("&")
}
}
} while (!scanner.isAtEnd)
return result
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.