HTML character decoding in Objective-C / Cocoa Touch

后端 未结 13 2181
我寻月下人不归
我寻月下人不归 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条回答
  •  借酒劲吻你
    2020-11-22 11:06

    This is the way I do it using RegexKitLite framework:

    -(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.

提交回复
热议问题