URL エンコード・デコード
URL 文字列をエンコードおよびデコードし、特殊文字や非ASCIIテキストを安全に処理します。クエリパラメータやパスの転送に適しています。
使用方法
URL エンコードは、特殊文字や非ASCII文字を %XX 形式に変換して、URL 内で安全に伝送できるようにします。
「エンコード」ボタンを使用して、テキストを URL 用の安全な形式(例:クエリパラメータ)に変換します。
「デコード」ボタンを使用して、%XX シーケンスを元の文字に戻します。
主な使用シーン:クエリパラメータの処理、フォームデータの送信、API 呼び出しなど。
機能特性
- 2種類のエンコードモード:encodeURIComponent と encodeURI
- 柔軟なスペース処理:+ または %20 にエンコード可能
- リアルタイム変換と入出力長の統計表示
- コピー・クリアボタンと双方向のペイン送信機能
- 完全な国際化対応と DaisyUI マルチテーマサポート
URL エンコードとは
URL エンコードは、文字を %XX のパーセントエンコードシーケンスに変換し、URL で安全に転送できるようにします。
encodeURIComponent は単一のコンポーネント(例:クエリパラメータの値)に適しており、ほとんどの英数字以外の文字をエンコードします。
encodeURI は完全な URI 文字列に適しており、?、&、=、/ などの構造的意味を持つ予約文字はエンコードしません。
主な用途
- クエリパラメータ(検索キーワード、フィルター条件など)の構築
- GET/POST フォームデータのエンコードと送信
- API リクエストパラメータのエンコード
- ユーザー入力を URL パスセグメントに安全に組み込む
よくある質問と注意点
- encodeURI と encodeURIComponent:パラメータには component、全体の URL には 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);