urldecode
urldecode(string $string): string
解码已编码的 URL 字符串:%XX 十六进制转义还原为字符,加号还原为空格。
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;
}
输出
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;
}
输出
a b&c?d
C# v11.0.100
v11.0.100
Console.WriteLine(Uri.UnescapeDataString("a+b%26c%3Fd"));
输出
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)
}
输出
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));
}
}
输出
a b&c?d
JavaScript v26.13.1
v26.13.1
console.log(decodeURIComponent("a+b%26c%3Fd"));
输出
a+b&c?d
PHP v8.6.0
v8.6.0
echo urldecode("a+b%26c%3Fd");
输出
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))
输出
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());
}
输出
a b&c?d
TypeScript v26.13.1
v26.13.1
const input: string = "a+b%26c%3Fd";
console.log(decodeURIComponent(input));
输出
a+b&c?d
语言差异说明
C:手写实现(+ 口径)。
C++:手写实现(+ 口径)。
C#:只处理 %XX、保留 + 原样:Uri.UnescapeDataString。
Go:+ 口径(把 + 还原为空格):url.QueryUnescape;非法序列返回错误——数据来自外部时务必校验。
Java:+ 口径 URLDecoder.decode。
JavaScript:只处理 %XX、保留 + 原样:decodeURIComponent;非法序列:decodeURIComponent("%ZZ") 抛 URIError(严格);自洽组合:encodeURIComponent 产出的串配 decodeURIComponent(+ 永不出现),表单串 a+b 必须用 + 口径解,误用 decodeURIComponent 会得到字面 a+b。
PHP:解码口径必须与编码方成对选择,否则出经典 bug——+ 口径(把 + 还原为空格):urldecode;非法序列行为:宽松,对非法转义原样保留;字节与字符串:%XX 解出的是字节,多字节 UTF-8(如 %E4%BD%A0)需整体解码后再按 UTF-8 解释。
Python:+ 口径 unquote_plus;只处理 %XX、保留 + 原样:unquote(双口径打印可见两行差异)。
Rust:手写实现(+ 口径);对非法转义原样保留(宽松)。
TypeScript:同 JS,decodeURIComponent。