URL エンコード/デコードツール
よくある質問
URL エンコードとは何ですか?
URL エンコードは、文字をインターネット経由で送信できる形式に変換する方法です。URL は ASCII 文字セットを使ってのみインターネット上で送信できます。URL には ASCII セット外の文字が含まれることが多いため、変換が必要です。URL エンコードでは、安全でない ASCII 文字を '%' に続いて 2 桁の 16 進数に置き換えます。
URL エンコードはどのように機能しますか?
URL エンコードは、安全でない文字を '%' に続いて文字の UTF-8 エンコーディングを表す 2 桁の 16 進数に置き換えることで機能します。例えば、スペース文字は %20 としてエンコードされます。URL にスペースを含めることはできないため、プラス記号 (+) または %20 に置き換えられます。その他の特殊文字は、対応する %xx コードに置き換えられます。
URL エンコードが必要な一般的な文字
URL で使用する際に URL エンコードが必要な一般的な文字は次のとおりです:
文字 | URL エンコード | 説明 |
---|---|---|
Space | %20 | URL エンコードが必要な最も一般的な文字 |
! | %21 | 感嘆符 |
" | %22 | 二重引用符 |
# | %23 | シャープ記号(URL フラグメントに使用) |
$ | %24 | ドル記号 |
% | %25 | パーセント記号(URL エンコードのエスケープ文字) |
& | %26 | アンパサンド(URL パラメータの区切りに使用) |
' | %27 | 一重引用符 |
( | %28 | 開き括弧 |
) | %29 | 閉じ括弧 |
+ | %2B | プラス記号 |
, | %2C | カンマ |
/ | %2F | スラッシュ(URL パスセパレータ) |
= | %3D | 等号(URL パラメータ割り当て) |
? | %3F | 疑問符(URL クエリ文字列の開始) |
さまざまなプログラミング言語での URL エンコードの実装
以下にさまざまなプログラミング言語での URL エンコードとデコードの例を示します:
Go
package main
import (
"fmt"
"net/url"
)
func main() {
// Encode a URL
text := "Hello World! Special chars: &?=/";
encoded := url.QueryEscape(text)
fmt.Println("Encoded:", encoded)
// Decode a URL
decoded, err := url.QueryUnescape(encoded)
if err == nil {
fmt.Println("Decoded:", decoded)
}
}
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Function to URL-encode a string
char *url_encode(char *str) {
char *encoded = malloc(strlen(str) * 3 + 1);
char *pstr = str;
char *pbuf = encoded;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
*pbuf++ = *pstr;
} else if (*pstr == ' ') {
*pbuf++ = '+';
} else {
sprintf(pbuf, "%%%.2X", *pstr);
pbuf += 3;
}
pstr++;
}
*pbuf = '\\0';
return encoded;
}
int main() {
char *text = "Hello World! Special chars: &?=/";
char *encoded = url_encode(text);
printf("Original: %s\\n", text);
printf("Encoded: %s\\n", encoded);
free(encoded);
return 0;
}
PHP
<?php
// URL encoding
$text = "Hello World! Special chars: &?=/";
$encoded = urlencode($text);
echo "Encoded: " . $encoded . "\\n";
// URL decoding
$decoded = urldecode($encoded);
echo "Decoded: " . $decoded . "\\n";
?>
Python
import urllib.parse
# URL encoding
text = "Hello World! Special chars: &?=/"
encoded = urllib.parse.quote(text)
print(f"Encoded: {encoded}")
# URL decoding
decoded = urllib.parse.unquote(encoded)
print(f"Decoded: {decoded}")
JavaScript
// URL encoding
const text = "Hello World! Special chars: &?=/";
const encoded = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded = decodeURIComponent(encoded);
console.log("Decoded:", decoded);
TypeScript
// URL encoding
const text: string = "Hello World! Special chars: &?=/";
const encoded: string = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded: string = decodeURIComponent(encoded);
console.log("Decoded:", decoded);
encodeURI と encodeURIComponent の違いは何ですか?
encodeURI() は完全な URI をエンコードするように設計されており、URL 内で特別な意味を持つ文字(/、?、:、@、&、=、+、$、# など)はエンコードしません。 対照的に、encodeURIComponent() はすべての特殊文字をエンコードするため、クエリ文字列パラメータなど URL の一部をエンコードするのに適しています。 クエリパラメータをエンコードする場合は、すべての特殊文字が適切にエンコードされるように、常に encodeURIComponent() を使用してください。