简体   繁体   English

URL编码和HTML编码NSStrings

[英]URL-encoding and HTML-encoding NSStrings

Is their a method to encode/decode HTML and URL (in Xcode, using Objective-C)? 他们是一种编码/解码HTML和URL的方法(在Xcode中,使用Objective-C)?

[NSString stringWithContentsOfFile:<#(NSString *)path#> encoding:<#(NSStringEncoding)enc#> error:<#(NSError **)error#>]

This doesn't seem to work how i expected. 这似乎不符合我的预期。 I thought it will convert special characters like "<" to equivalent HTML entities ie "<" in this case. 我认为它会将像“<”这样的特殊字符转换为等效的HTML实体,在这种情况下是“<”。

Here's a reference to the w3school link related to this topic (general): 以下是与该主题相关的w3school链接的参考(一般):

HTML URL Encoding Reference HTML URL编码参考

HTML Entities Reference HTML实体参考

Thanking in anticipation. 感谢期待。

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. 使用给定的编码返回接收器的表示,以确定将接收器转换为合法URL字符串所需的转义百分比。

- (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). 您引用的方法使用给定的字符编码(例如UTF-8或ASCII)从磁盘读取文件。 It has nothing to do with URL or HTML escaping. 它与URL或HTML转义无关。

If you want to add URL percent escapes, you want this method: 如果要添加URL百分比转义,则需要以下方法:

[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() . 在某些情况下,您可能必须使用更复杂但更灵活的CFURLCreateStringByAddingPercentEscapes() (If you do, note that you can cast CFStringRef to NSString * and vice versa.) (如果这样做,请注意您可以将CFStringRefNSString * ,反之亦然。)

There's nothing built in that I know of to do XML/HTML-style entity escaping, but this function ought to handle the basics: 我知道没有内置的XML / HTML风格的实体转义,但是这个函数应该处理基础知识:

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

    [temp replaceOccurrencesOfString:@"&"
                          withString:@"&amp;"
                             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: 要进行HTML / XML实体编码,可以使用CFMutableString函数:

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. 通过将CFStringTransform的最后一个参数设置为true,它也适用于解码(十六进制)实体。

Use CFStringTransform for HTML entity encoding/decoding: 使用CFStringTransform进行HTML实体编码/解码:

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

You need to use the ICU transform "Any-Hex/XML". 您需要使用ICU转换“Any-Hex / XML”。 kCFStringTransformToXMLHex isn't aggressive enough. kCFStringTransformToXMLHex不够激进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM