parse_str
(PHP 4, PHP 5, PHP 7)
parse_str — 文字列を処理し、変数に代入する
説明
$encoded_string
[, array &$result
] ) : void
URL 経由で渡されるクエリ文字列と同様に encoded_string
を処理し、現在のスコープに変数をセットします。
(or in the array if result is provided)
パラメータ
-
encoded_string -
入力文字列。
-
result -
2 番目の引数
resultが指定された場合、 変数は、代わりに配列の要素としてこの変数に保存されます。警告Using this function without the
resultparameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.Dynamically setting variables in function's scope suffers from exactly same problems as register_globals.
Read section on security of Using Register Globals explaining why it is dangerous.
返り値
値を返しません。
変更履歴
| バージョン | 説明 |
|---|---|
| 7.2.0 |
Usage of parse_str() without a second parameter
now emits an E_DEPRECATED notice.
|
例
例1 parse_str() の使用法
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
// Recommended
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
// DISCOURAGED
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
?>
Because variables in PHP can't have dots and spaces in their names,
those are converted to underscores. Same applies to naming of
respective key names in case of using this function with
result parameter.
例2 parse_str() name mangling
<?php
parse_str("My Value=Something");
echo $My_Value; // Something
parse_str("My Value=Something", $output);
echo $output['My_Value']; // Something
?>
注意
注意:
All variables created (or values returned into array if second parameter is set) are already urldecode()d.
注意:
現在の QUERY_STRING を取得するには、変数 $_SERVER['QUERY_STRING'] を使用する事ができます。また、 外部から来る変数 のセクションも読んでください。
注意:
magic_quotes_gpc の設定が、この関数の出力に影響を与えます。というのも parse_str() が使用している仕組みは PHP が $_GET や $_POST などの設定に使用しているものと同じだからです。
参考
- parse_url() - URL を解釈し、その構成要素を返す
- pathinfo() - ファイルパスに関する情報を返す
- http_build_query() - URL エンコードされたクエリ文字列を生成する
- urldecode() - URL エンコードされた文字列をデコードする