Objective C HTML escape/unescape

前端 未结 14 1954
生来不讨喜
生来不讨喜 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条回答
  • 2020-11-22 17:08

    Why not just using ?

    NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    return result;
    

    Noob question but in my case it works...

    0 讨论(0)
  • 2020-11-22 17:11

    This is an easy to use NSString category implementation:

    • http://code.google.com/p/qrcode-scanner-live/source/browse/trunk/iphone/Classes/NSString%2BHTML.h
    • http://code.google.com/p/qrcode-scanner-live/source/browse/trunk/iphone/Classes/NSString%2BHTML.m

    It is far from complete but you can add some missing entities from here: http://code.google.com/p/statz/source/browse/trunk/NSString%2BHTML.m

    Usage:

    #import "NSString+HTML.h"
    
    NSString *raw = [NSString stringWithFormat:@"<div></div>"];
    NSString *escaped = [raw htmlEscapedString];
    
    0 讨论(0)
提交回复
热议问题