Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL-encoding and HTML-encoding NSStrings

Is their a method to encode/decode HTML and URL (in Xcode, using 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 i.e. "<" in this case.

Here's a reference to the w3school link related to this topic (general):

HTML URL Encoding Reference

HTML Entities Reference

Thanking in anticipation.

like image 403
Mustafa Avatar asked Nov 03 '09 13:11

Mustafa


People also ask

What is the difference between URL encoding and HTML encoding?

HTMLEncoding turns this character into "&lt;" which is the encoded representation of the less-than sign. URLEncoding does the same, but for URLs, for which the special characters are different, although there is some overlap.

What is HTML URL encoding?

URL encoding converts non-ASCII characters into a format that can be transmitted over the Internet. URL encoding replaces non-ASCII characters with a "%" followed by hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign, or %20.

What type of encoding is URL encoding?

Percent-encoding is a mechanism to encode 8-bit characters that have specific meaning in the context of URLs. It is sometimes called URL encoding. The encoding consists of substitution: A '%' followed by the hexadecimal representation of the ASCII value of the replace character.

What is %20 URL encode?

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.


2 Answers

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
like image 105
zaph Avatar answered Oct 02 '22 02:10

zaph


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:@"&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];
}
like image 33
Becca Royal-Gordon Avatar answered Oct 02 '22 02:10

Becca Royal-Gordon