0 字元

:

:

:

// 百分比編碼的字元

字元編碼結果
%20
!%21
#%23
$%24
&%26
+%2B
/%2F
:%3A
=%3D
?%3F
@%40
é%C3%A9
中%E4%B8%AD
😀%F0%9F%98%80

// 在程式碼中進行 URL 編碼

JavaScript   encodeURIComponent(s)
Python       urllib.parse.quote(s, safe='')
PHP          rawurlencode($s)
Go           url.QueryEscape(s)
Java         URLEncoder.encode(s, StandardCharsets.UTF_8)
curl         curl --data-urlencode "q=hello world" URL

>> 更多問題

問:encodeURI 和 encodeURIComponent 有什麼差別?

答:encodeURI 編碼整個 URL,保留 : / ? # & = 等結構字元。encodeURIComponent 編碼單一部分(如參數值),這些字元也會被跳脫。對查詢參數值幾乎總是應使用 encodeURIComponent。

問:為什麼 中文 或 é 會變成多組 % 編碼?

答:URL 傳輸的是位元組。字元先以 UTF-8 編碼(é 為 2 位元組,中 為 3 位元組,emoji 為 4 位元組),每個位元組寫成 %XX,例如 é → %C3%A9。

問:什麼是重複編碼(%2520)?

答:對已編碼的文字再次編碼時,% 會變成 %25,%20 就變成 %2520。這通常是錯誤:組合 URL 時每個值只應編碼一次。