<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>URLの一部に日本語を利用したい</title>
</head>
<body>
<form method="get" action="">
<p>文字列を入力し、リンクをクリックします<br />
<input type="text" name="data" />
<input type="submit" value="送信" />
</p>
</form>
<?php
if (isset($_GET['data'])) {
$string = $_GET['data'];
$urlEncode = urlencode($_GET['data']);
echo '元文字列: ' . h($string) . '<br />';
echo 'エンコード処理後: ' . $urlEncode . '<br />';
echo '<a href="' . $_SERVER['SCRIPT_NAME'] . '?data2=' . $urlEncode . '">リンク</a><br />';
}
if (isset($_GET['data2'])) {
$string = $_GET['data2'];
echo 'デコード処理後: ' . h($string);
}
function h($string) {
return htmlspecialchars($string, ENT_QUOTES);
}
?>
</body>
</html>