3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで意図せずURLにスペースが入る場合

Posted at

面倒を見ているシステムで、PHPでURLを取得するとなぜかスペースが入ることがあった。
何だろうと思って調べていった結果、URLデコードには二つの関数が存在するということに初めて気づいた。

 関数名   メモ 
urldecode +をスペースに変換する
rawurldecode +をスペースに変換しない

確認用コード

<?php

$urlArray = array(
    "url1" => "ai+eo",
    "url2" => "ai%2Beo",
);

echo "urldecode version:\n";
foreach($urlArray as $key => $value) {
	echo "$key($value) = " , urldecode($value), "\n";
}
echo "\n";
echo "rawurldecode version:\n";
foreach($urlArray as $key => $value) {
	echo "$key($value) = " , rawurldecode($value), "\n";
}

実行結果

[c:php]
$ php -f test.php
urldecode version:
url1(ai+eo) = ai eo
url2(ai%2Beo) = ai+eo

rawurldecode version:
url1(ai+eo) = ai+eo
url2(ai%2Beo) = ai+eo

システムの挙動が開発機と本番機で違ったからphp.iniでも動作が変わるかも・・・(未確認)

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?