How to properly URL encode a string in PHP?

For the URI query use urlencode/urldecode; for anything else use rawurlencode/rawurldecode. The difference between urlencode and rawurlencode is that urlencode encodes according to application/x-www-form-urlencoded (space is encoded with +) while rawurlencode encodes according to the plain Percent-Encoding (space is encoded with %20).

What is the proper way to URL encode Unicode characters?

I would always encode in UTF-8. From the Wikipedia page on percent encoding: The generic URI syntax mandates that new URI schemes that provide for the representation of character data in a URI must, in effect, represent characters from the unreserved set without translation, and should convert all other characters to bytes according to UTF-8, … Read more

How can I percent-encode URL parameters in Python?

Python 2 From the documentation: urllib.quote(string[, safe]) Replace special characters in string using the %xx escape. Letters, digits, and the characters ‘_.-‘ are never quoted. By default, this function is intended for quoting the path section of the URL.The optional safe parameter specifies additional characters that should not be quoted — its default value is … Read more

Server.UrlEncode vs. HttpUtility.UrlEncode

I had significant headaches with these methods before, I recommend you avoid any variant of UrlEncode, and instead use Uri.EscapeDataString – at least that one has a comprehensible behavior. Let’s see… HttpUtility.UrlEncode(” “) == “+” //breaks ASP.NET when used in paths, non- //standard, undocumented. Uri.EscapeUriString(“a?b=e”) == “a?b=e” // makes sense, but rarely what you // … Read more

urlencode vs rawurlencode?

It will depend on your purpose. If interoperability with other systems is important then it seems rawurlencode is the way to go. The one exception is legacy systems which expect the query string to follow form-encoding style of spaces encoded as + instead of %20 (in which case you need urlencode). rawurlencode follows RFC 1738 … Read more

URLEncoder not able to translate space character

This behaves as expected. The URLEncoder implements the HTML Specifications for how to encode URLs in HTML forms. From the javadocs: This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. and from the HTML Specification: application/x-www-form-urlencoded Forms submitted with this content type must be encoded as follows: Control names and … Read more

Objective-C and Swift URL encoding

To escape the characters you want is a little more work. Example code iOS7 and above: NSString *unescaped = @”http://www”; NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSLog(@”escapedString: %@”, escapedString); NSLog output: escapedString: http%3A%2F%2Fwww The following are useful URL encoding character sets: URLFragmentAllowedCharacterSet “#%<>[\]^`{|} URLHostAllowedCharacterSet “#%/<>?@\^`{|} URLPasswordAllowedCharacterSet “#%/:<>?@[\]^`{|} URLPathAllowedCharacterSet “#%;<>?[\]^`{|} URLQueryAllowedCharacterSet “#%<>[\]^`{|} URLUserAllowedCharacterSet “#%/:<>?@[\]^` Creating a … Read more

How to urlencode data for curl command?

Use curl –data-urlencode; from man curl: This posts data, similar to the other –data options with the exception that this performs URL-encoding. To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification. Example usage: curl \ –data-urlencode “paramName=value” \ –data-urlencode “secondParam=value” \ http://example.com See the man … Read more