#navi(../)
* PHPのURLエンコード関数・urlencode, rawurlencodeの調査 [#ya6aae17]
PHPのURLエンコード関数、urlencode()とrawurlencode()の2つを調査してみました。~
以下に調査結果を記します。~
尚、本記事のテストPHPスクリプトはコマンドライン(CLI:Command Line Interface)で試しました。

#contents
#htmlinsertpcsp(ll-top.html,ll-sp.html)

* 関連資料 [#af956fed]
PHP Manualのリンクです。
- [[rawurlencode>http://php.morva.net/manual/ja/function.rawurlencode.php]]
- [[urlencode>http://php.morva.net/manual/ja/function.urlencode.php]]

* 調査環境 [#x531cc51]
-PHP
 $ php --version
 PHP 5.3.3 (cli) (built: Jul  3 2012 16:53:21)
 Copyright (c) 1997-2010 The PHP Group
 Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
-OS
 $ lsb_release -d
 Description:    CentOS release 6.3 (Final)

* urlencodeとrawurlencodeを調査 [#fb1222c3]
上記の関連資料を見ると、半角スペースやチルダの取り扱いが異なるようです。~
** 半角スペースのURLエンコード [#la3e04cc]
実際に半角スペースのURLエンコードを実行してみます。
#ref(sp_encode.php.zip)
 <?php
  $str = " ";
  printf("%s\n", urlencode($str));
  printf("%s\n", rawurlencode($str));
 ?>
実行結果~
 $ php sp_encode.php
 +
 %20
** チルダのURLエンコード [#y4e48ce5]
#ref(tilde_encode.php.zip)
 <?php
  $str = "~";
  printf("%s\n", urlencode($str));
  printf("%s\n", rawurlencode($str));
 ?>
実行結果~
 [sakura@centos6 ~]$ php tilde_encode.php
 %7E
 ~
当方で確認していませんが、PHPのバージョンにより動作が異なるようです。
-''PHP 5.2.xまでは、&7Eに変換するそうです。''
-''PHP 5.3.0からは変換せずチルダのままのようです。''

以上、PHPのURLエンコード関数・urlencode, rawurlencodeの調査でした。

#htmlinsertpcsp(ll-btm.html,ll-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS