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