urldecode

urldecode(string $string): string

Decodes a URL-encoded string: %XX hexadecimal escapes become characters and plus signs become spaces.

C C23

C23
#include <stdio.h>

int hex_val(char c) {
    if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    return -1;
}

int main(void) {
    const char *s = "a+b%26c%3Fd";
    char out[64];
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        if (s[i] == '+') {
            out[j++] = ' ';
        } else if (s[i] == '%' && hex_val(s[i + 1]) >= 0 && hex_val(s[i + 2]) >= 0) {
            out[j++] = (char)(hex_val(s[i + 1]) * 16 + hex_val(s[i + 2]));
            i += 2;
        } else {
            out[j++] = s[i];
        }
    }
    out[j] = '\0';
    printf("%s\n", out);
    return 0;
}
Output
a b&c?d

C++ C++26

C++26
#include <iostream>
#include <string>

int hex_val(char c) {
    if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    return -1;
}

int main() {
    std::string s = "a+b%26c%3Fd";
    std::string out;
    for (size_t i = 0; i < s.size(); i++) {
        if (s[i] == '+') {
            out += ' ';
        } else if (s[i] == '%' && i + 2 < s.size() && hex_val(s[i + 1]) >= 0 && hex_val(s[i + 2]) >= 0) {
            out += (char)(hex_val(s[i + 1]) * 16 + hex_val(s[i + 2]));
            i += 2;
        } else {
            out += s[i];
        }
    }
    std::cout << out << "\n";
    return 0;
}
Output
a b&c?d

C# v11.0.100

v11.0.100
Console.WriteLine(Uri.UnescapeDataString("a+b%26c%3Fd"));
Output
a+b&c?d

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "net/url"
)

func main() {
    decoded, _ := url.QueryUnescape("a+b%26c%3Fd")
    fmt.Println(decoded)
}
Output
a b&c?d

Java v27.0.1

v27.0.1
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        System.out.println(URLDecoder.decode("a+b%26c%3Fd", StandardCharsets.UTF_8));
    }
}
Output
a b&c?d

JavaScript v26.13.1

v26.13.1
console.log(decodeURIComponent("a+b%26c%3Fd"));
Output
a+b&c?d

PHP v8.6.0

v8.6.0
echo urldecode("a+b%26c%3Fd");
Output
a b&c?d

Python v3.15.2

v3.15.2
from urllib.parse import unquote, unquote_plus

s = "a+b%26c%3Fd"
print(unquote(s), unquote_plus(s))
Output
a+b&c?d a b&c?d

Rust v1.99.1

v1.99.1
fn main() {
    let s = "a+b%26c%3Fd";
    let bytes = s.as_bytes();
    let mut out: Vec<u8> = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'+' => {
                out.push(b' ');
                i += 1;
            }
            b'%' if i + 2 < bytes.len()
                && bytes[i + 1].is_ascii_hexdigit()
                && bytes[i + 2].is_ascii_hexdigit() =>
            {
                let hi = (bytes[i + 1] as char).to_digit(16).unwrap() as u8;
                let lo = (bytes[i + 2] as char).to_digit(16).unwrap() as u8;
                out.push(hi * 16 + lo);
                i += 3;
            }
            b => {
                out.push(b);
                i += 1;
            }
        }
    }
    println!("{}", String::from_utf8(out).unwrap());
}
Output
a b&c?d

TypeScript v26.13.1

v26.13.1
const input: string = "a+b%26c%3Fd";
console.log(decodeURIComponent(input));
Output
a+b&c?d

Language differences

C: hand-written implementation (the plus dialect).

C++: hand-written implementation (the plus dialect).

C#: the %XX-only decoder that leaves plus untouched — Uri.UnescapeDataString.

Go: the plus dialect — url.QueryUnescape; invalid sequences return an error, so always validate externally supplied data.

Java: the plus dialect — URLDecoder.decode.

JavaScript: the %XX-only decoder — decodeURIComponent; invalid sequences: decodeURIComponent("%ZZ") throws URIError (strict); self-consistent pairing: encodeURIComponent output pairs with decodeURIComponent (no plus ever appears), and a form string a+b must be decoded with the plus dialect — misusing decodeURIComponent yields the literal a+b.

PHP: the decode dialect must pair with the encoder or the classic bug appears — the plus dialect: urldecode; invalid sequences are handled leniently and pass through; bytes vs strings: %XX yields raw bytes, and multi-byte UTF-8 (e.g. %E4%BD%A0) must be decoded as a whole and then interpreted as UTF-8.

Python: plus dialect unquote_plus; %XX-only unquote (the dual output shows both lines).

Rust: hand-written (the plus dialect); invalid escapes pass through (lenient).

TypeScript: same as JS — decodeURIComponent.