Objective C HTML escape/unescape

前端 未结 14 2139
生来不讨喜
生来不讨喜 2020-11-22 16:19

Wondering if there is an easy way to do a simple HTML escape/unescape in Objective C. What I want is something like this psuedo code:

NSString *string = @\"         


        
14条回答
  •  猫巷女王i
    2020-11-22 17:03

    This link contains the solution below. Cocoa CF has the CFXMLCreateStringByUnescapingEntities function but that's not available on the iPhone.

    @interface MREntitiesConverter : NSObject {
        NSMutableString* resultString;
    }
    
    @property (nonatomic, retain) NSMutableString* resultString;
    
    - (NSString*)convertEntitiesInString:(NSString*)s;
    
    @end
    
    
    @implementation MREntitiesConverter
    
    @synthesize resultString;
    
    - (id)init
    {
        if([super init]) {
            resultString = [[NSMutableString alloc] init];
        }
        return self;
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s {
            [self.resultString appendString:s];
    }
    
    - (NSString*)convertEntitiesInString:(NSString*)s {
        if (!s) {
            NSLog(@"ERROR : Parameter string is nil");
        }
        NSString* xmlStr = [NSString stringWithFormat:@"%@", s];
        NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
        NSXMLParser* xmlParse = [[[NSXMLParser alloc] initWithData:data] autorelease];
        [xmlParse setDelegate:self];
        [xmlParse parse];
        return [NSString stringWithFormat:@"%@",resultString];
    }
    
    - (void)dealloc {
        [resultString release];
        [super dealloc];
    }
    
    @end
    

提交回复
热议问题