Skip to main content

URL Encode & Decode in JavaScript, PHP, Java

Updated 22 October 2026 · 3 min read

Every language has a function for this, but they differ in one important way: some write a space as %20 and some as +. Below is the right call for each language, using the same text, tea & coffee, so the results are easy to compare.

Free toolURL Encoder & DecoderOpen

Quick answer

LanguageEncode a valueDecode a value
JavaScriptencodeURIComponent(s)decodeURIComponent(s)
PHPrawurlencode($s)rawurldecode($s)
JavaURLEncoder.encode(s, UTF_8)URLDecoder.decode(s, UTF_8)
Pythonurllib.parse.quote(s, safe="")urllib.parse.unquote(s)
C# / .NETUri.EscapeDataString(s)Uri.UnescapeDataString(s)
RubyURI.encode_www_form_component(s)URI.decode_www_form_component(s)
Rusturlencoding::encode(s)urlencoding::decode(s)
Dart / FlutterUri.encodeComponent(s)Uri.decodeComponent(s)

Decode URL in JavaScript

JavaScript
encodeURIComponent("tea & coffee");        // "tea%20%26%20coffee"
decodeURIComponent("tea%20%26%20coffee");   // "tea & coffee"

// Read the parameters of a link
const url = new URL("https://example.com/search?q=tea%20%26%20coffee&page=2");
url.searchParams.get("q");     // "tea & coffee"

// Build a query string (spaces become +)
new URLSearchParams({ q: "tea & coffee" }).toString();  // "q=tea+%26+coffee"

decodeURIComponent throws a URIError if the text has a bad % code, for example a lone %. Wrap it in try and catch when the text comes from a user. Use encodeURI and decodeURI only for a whole address, since they leave : / ? & = # alone.

URL encode and decode in PHP

PHP
rawurlencode("tea & coffee");   // "tea%20%26%20coffee" (space is %20)
urlencode("tea & coffee");      // "tea+%26+coffee"     (space is +)
rawurldecode("tea%20%26%20coffee");
urldecode("tea+%26+coffee");      // "tea & coffee"

// Decode the query string of a link into an array
parse_str("q=tea%20%26%20coffee&page=2", $params);
// $params["q"] is "tea & coffee"

// Build a query string
http_build_query(["q" => "tea & coffee"]);  // "q=tea+%26+coffee"

Use rawurlencode for paths and urlencode for form-style values. urldecode also turns + into a space, which is right for query strings and wrong for a path.

URL encode and decode in Java

Java
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

String e = URLEncoder.encode("tea & coffee", StandardCharsets.UTF_8);
// "tea+%26+coffee"  (URLEncoder writes + for a space)
String path = e.replace("+", "%20");   // "tea%20%26%20coffee"

String d = URLDecoder.decode("tea+%26+coffee", StandardCharsets.UTF_8);
// "tea & coffee"

The version that takes StandardCharsets exists from Java 10. On older Java, pass "UTF-8" as a string and handle UnsupportedEncodingException. Always give a charset: the default can differ by machine.

Python

Python
from urllib.parse import quote, quote_plus, unquote, unquote_plus, parse_qs

quote("tea & coffee", safe="")   # 'tea%20%26%20coffee'
quote_plus("tea & coffee")      # 'tea+%26+coffee'
unquote("tea%20%26%20coffee")    # 'tea & coffee'
unquote_plus("tea+%26+coffee")   # 'tea & coffee'

parse_qs("a=1&b=x%20y&a=2")      # {'a': ['1', '2'], 'b': ['x y']}

quote leaves / alone by default. Pass safe="" when you are encoding one value, so a slash inside it is encoded too.

C# and .NET

C#
Uri.EscapeDataString("tea & coffee");          // "tea%20%26%20coffee"
Uri.UnescapeDataString("tea%20%26%20coffee");  // "tea & coffee"

// System.Net.WebUtility (space becomes +)
WebUtility.UrlEncode("tea & coffee");           // "tea+%26+coffee"
WebUtility.UrlDecode("tea+%26+coffee");         // "tea & coffee"

Ruby

Ruby
require "uri"
require "erb"

URI.encode_www_form_component("tea & coffee")   # "tea+%26+coffee"
URI.decode_www_form_component("tea+%26+coffee")  # "tea & coffee"
ERB::Util.url_encode("tea & coffee")             # "tea%20%26%20coffee"

Older tutorials use URI.encode. It was removed in Ruby 3.0, so use the calls above.

Rust

Rust (urlencoding crate)
// Cargo.toml: urlencoding = "2"
use urlencoding::{decode, encode};

let e = encode("tea & coffee");                 // "tea%20%26%20coffee"
let d = decode("tea%20%26%20coffee").unwrap();   // "tea & coffee"

decode returns a Result, because the bytes may not be valid UTF-8. The percent-encoding crate gives finer control over which characters are encoded.

Dart and Flutter

Dart
Uri.encodeComponent("tea & coffee");           // "tea%20%26%20coffee"
Uri.decodeComponent("tea%20%26%20coffee");     // "tea & coffee"

// Whole address: keeps : / ? & = #
Uri.encodeFull("https://example.com/a b");     // "https://example.com/a%20b"

Check your result without writing code

When you are not sure what a function should return, paste the text into the URL Encoder & Decoder. Component matches encodeURIComponent, Full URL matches encodeURI, and the plus option matches the form-style functions such as urlencode and quote_plus.

Encode each value separately and put the encoded values into the link yourself. Encoding a whole finished link in one go breaks the : / ? and & that make it work.

Try it now: URL Encoder & Decoder

Decode an encoded URL into readable text, or encode text so it is safe inside a link. Query parameters are listed for you.

Open URL Encoder & Decoder