URL-encoding and HTML-encoding NSStrings

妖精的绣舞 提交于 2019-11-30 19:03:08

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

and

Returns a new string made by replacing in the receiver all percent escapes with the matching characters as determined by a given encoding.

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

The method you cite reads a file from disk with a given character encoding (such as UTF-8 or ASCII). It has nothing to do with URL or HTML escaping.

If you want to add URL percent escapes, you want this method:

[myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

Make sure you read the documentation about this method, because there are certain subtleties about what it escapes and what it leaves alone. In some cases, you may have to use the more complex, but more flexible, CFURLCreateStringByAddingPercentEscapes(). (If you do, note that you can cast CFStringRef to NSString * and vice versa.)

There's nothing built in that I know of to do XML/HTML-style entity escaping, but this function ought to handle the basics:

NSString * convertToXMLEntities(NSString * myString) {
    NSMutableString * temp = [myString mutableCopy];

    [temp replaceOccurrencesOfString:@"&"
                          withString:@"&"
                             options:0
                               range:NSMakeRange(0, [temp length])];
    [temp replaceOccurrencesOfString:@"<"
                          withString:@"&lt;"
                             options:0
                               range:NSMakeRange(0, [temp length])];
    [temp replaceOccurrencesOfString:@">"
                          withString:@"&gt;"
                             options:0
                               range:NSMakeRange(0, [temp length])];
    [temp replaceOccurrencesOfString:@"\""
                          withString:@"&quot;"
                             options:0
                               range:NSMakeRange(0, [temp length])];
    [temp replaceOccurrencesOfString:@"'"
                          withString:@"&apos;"
                             options:0
                               range:NSMakeRange(0, [temp length])];

    return [temp autorelease];
}

To do HTML/XML entity encoding, you can use a CFMutableString function:

NSString *result = .....;
CFStringTransform((CFMutableStringRef)result, NULL, kCFStringTransformToXMLHex, false);

By setting the last parameter of CFStringTransform to true, it should work for decoding (hex) entities as well.

rosstulloch

Use CFStringTransform for HTML entity encoding/decoding:

CFStringTransform((CFTypeRef)yourMutableString, NULL, CFSTR("Any-Hex/XML"), FALSE );

You need to use the ICU transform "Any-Hex/XML". kCFStringTransformToXMLHex isn't aggressive enough.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!