URL 编码与解码工具

解码 URL

关于 URL 编码

什么是 URL 编码?

URL 百分号编码用 %HH 字节转义表示 UTF-8 文本。本工具遵循 encodeURIComponent 的行为:字母、数字和 -_.!~*'() 保持原样,其他字节(包括 URL 分隔符和非 ASCII 文本)会被转义。

为什么使用 URL 编码?

应在组装 URL 前分别编码路径段或查询值,避免空格、&、=、? 和 Unicode 改变 URL 结构。本工具不会解析完整 URL,也不会把 + 当作空格;它会拒绝格式错误的 % 序列,并且编码不等于加密。

Bash 中的 URL 编码

# Encoding
printf 'Hello, World!' | jq -sRr @uri
# Output: Hello%2C%20World%21

# Decoding
printf 'Hello%2C%20World%21' | jq -sRr @urid
# Output: Hello, World!
			

JavaScript 中的 URL 编码

// Encoding
let encoded = encodeURIComponent("Hello, World!");
console.log(encoded);
// Output: Hello%2C%20World%21

// Decoding
let decoded = decodeURIComponent("Hello%2C%20World%21");
console.log(decoded);
// Output: Hello, World!
			

Go 语言中的 URL 编码

package main
import (
	"fmt"
	"net/url"
)
func main() {
	// Encoding
	encoded := url.QueryEscape("Hello, World!")
	fmt.Println(encoded)
	// Output: Hello%2C+%21World%21 (QueryEscape uses + for spaces)

	// Decoding
	decoded, _ := url.QueryUnescape("Hello%2C%20World%21")
	fmt.Println(decoded)
	// Output: Hello, World!
}
			

PHP 中的 URL 编码

// Encoding
$encoded = urlencode("Hello, World!");
echo $encoded . "\n";
// Output: Hello%2C+%21World%21

// Decoding
$decoded = urldecode("Hello%2C%20World%21");
echo $decoded . "\n";
// Output: Hello, World!