URL 编解码

对 URL 字符串进行编码与解码,安全处理特殊字符与非 ASCII 文本,适用于查询参数与路径传输。

编码

编码模式

解码

使用说明

URL 编码将特殊字符或非 ASCII 字符转换为 %XX 形式,以便在 URL 中安全传输。
使用“编码”将文本转换为适用于 URL 的安全形式(例如查询参数)。
使用“解码”将 %XX 序列还原为原始字符。
常见场景:处理查询参数、表单数据传输、API 调用等。

快速示例

原文本
https://example.com/search?q=你好世界&type=text
URL 编码
https://example.com/search?q=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C&type=text

功能特性

  • 两种编码模式:encodeURIComponent 与 encodeURI
  • 灵活的空格处理:编码为 + 或 %20
  • 实时转换并显示输入/输出长度统计
  • 复制/清空按钮与双向发送到对方面板
  • 完整的国际化与 DaisyUI 多主题支持

什么是 URL 编码

URL 编码将字符转换为 %XX 百分号编码序列,以便在 URL 中安全传输。

encodeURIComponent 适用于单个组件(如单个查询参数值),会编码绝大多数非字母数字字符。

encodeURI 适用于完整 URI 字符串,不会编码 ?, &, =, / 等具有结构意义的保留字符。

常见用途

  • 构建查询参数(搜索关键词、筛选条件等)
  • GET/POST 表单数据的编码传输
  • API 请求参数的编码
  • 将用户输入安全地放入 URL 路径片段

常见问题与坑

  • encodeURI vs encodeURIComponent:参数用 component,整条链接用 URI
  • 空格与 +:application/x-www-form-urlencoded 在查询串中常用 + 表示空格
  • 避免重复解码:多次解码会破坏数据
  • 无效的百分号序列会抛错;需要优雅地处理异常

如何在编程语言中进行 URL 编解码

JavaScript
编码
// Component (recommended for query values)
const encoded = encodeURIComponent(text);
// If you need '+' for spaces in query strings
const encodedPlus = encoded.replace(/%20/g, '+');
// Full URI
const encodedUri = encodeURI(url);
解码
// Treat '+' as space if needed
const input = plusAsSpace ? s.replace(/\+/g, ' ') : s;
// Component
const dec1 = decodeURIComponent(input);
// Fallback to decodeURI when input is a full URL
const dec2 = decodeURI(input);
PHP
编码
// Component (RFC 3986)
$encoded = rawurlencode($text);
// '+' for spaces (application/x-www-form-urlencoded)
$encodedPlus = str_replace('%20', '+', $encoded);
// Full query usage
$query = http_build_query(['q' => '你好 世界', 'type' => 'text']);
解码
// Component
$decoded = rawurldecode($s);
// If input uses '+' for spaces
$decodedPlus = urldecode($s);
Python
编码
from urllib.parse import quote, quote_plus

# Component (RFC 3986)
enc = quote(text, safe='')
# '+' for spaces (application/x-www-form-urlencoded)
enc_plus = quote_plus(text)
解码
from urllib.parse import unquote, unquote_plus

# Component
dec = unquote(s)
# Input with '+' for spaces
dec_plus = unquote_plus(s)
Go
编码
import (
    "net/url"
)

// Query component
enc := url.QueryEscape(text) // spaces => +
// Path segment
// go1.8+
// url.PathEscape(text)
解码
import (
    "net/url"
)

// Query component
dec, _ := url.QueryUnescape(s)
// Path segment
// url.PathUnescape(s)
Rust
编码
// Cargo.toml: urlencoding = "^2"
use urlencoding::{encode, encode_binary};

let enc = encode(text);              // component; spaces => %20
let enc_plus = enc.replace("%20", "+"); // if you need '+' for spaces
解码
use urlencoding::decode;

// decode treats '+' as space
let dec = decode(s)?; // Result, _>
Java
编码
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

// Query param / form style (spaces => '+')
String enc = URLEncoder.encode(text, StandardCharsets.UTF_8);
// For RFC3986-like component encoding, replace '+' with %20 if needed
String encSpace = enc.replace("+", "%20");
解码
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

// URLDecoder treats '+' as space
String dec = URLDecoder.decode(s, StandardCharsets.UTF_8);