(PHP 4, PHP 5, PHP 7, PHP 8)
explode — 文字列を文字列により分割する
文字列の配列を返します。この配列の各要素は、
string を文字列
separator で区切った部分文字列となります。
separator区切り文字列。
string入力文字列。
limit
limit に正の値が指定された場合、返される配列には
最大 limit の要素が含まれ、その最後の要素には
string の残りの部分が全て含まれます。
もし limit パラメータが負の場合、
最後の -limit 個の要素を除く全ての構成要素が返されます。
limit パラメータがゼロの場合は、1 を指定したものとみなされます。
注意:
PHP 8.0 より前のバージョンでは、implode() はいずれのパラメータ順も受け入れることができていましたが、 explode() はそのようなことはサポートしていません。 つまり、
string引数の前に必ずseparator引数がくることを確認しなければいけません。
string の内容を
separator で分割した文字列の配列を返します。
空の文字列 ("") が separator として使用された場合、
explode() は ValueError
をスローします。
separator に引数
string に含まれていない値が含まれている場合は、
limit が負の値なら空の配列、そうでなければ
引数 string を含む配列を返します。
separator の値が string
の最初か最後に現れた場合、その位置に応じて、
返される配列の最初か最後に空の配列が追加されます。
| バージョン | 説明 |
|---|---|
| 8.0.0 |
引数 separator に空文字列 ("") を渡した場合、
ValueError をスローするようになりました。
それより前のバージョンでは、
explode() は false を返していました。
|
例1 explode() の例
<?php
// 例 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0], PHP_EOL; // piece1
echo $pieces[1], PHP_EOL; // piece2
// 例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user, PHP_EOL; // foo
echo $pass, PHP_EOL; // *
?>
例2 explode() の戻り値の例
<?php
/*
デリミタを含まない文字列の場合は、
単に元の文字列だけを含む一要素の配列を返します
*/
$input1 = "hello";
$input2 = "hello,there";
$input3 = ',';
var_dump( explode( ',', $input1 ) );
var_dump( explode( ',', $input2 ) );
var_dump( explode( ',', $input3 ) );
?>上の例の出力は以下となります。
array(1) ( [0] => string(5) "hello" ) array(2) ( [0] => string(5) "hello" [1] => string(5) "there" ) array(2) ( [0] => string(0) "" [1] => string(0) "" )
例3 limit パラメータの例
<?php
$str = 'one|two|three|four';
// 正の値を持つ limit
print_r(explode('|', $str, 2));
// 負の値を持つ limit
print_r(explode('|', $str, -1));
?>上の例の出力は以下となります。
Array ( [0] => one [1] => two|three|four ) Array ( [0] => one [1] => two [2] => three )
注意: この関数はバイナリデータに対応しています。
Note that an empty input string will still result in one element in the output array. This is something to remember when you are processing unknown input.
For example, maybe you are splitting part of a URI by forward slashes (like "articles/42/show" => ["articles", "42", "show"]). And maybe you expect that an empty URI will result in an empty array ("" => []). Instead, it will contain one element, with an empty string:
<?php
$uri = '';
$parts = explode('/', $uri);
var_dump($parts);
?>
Will output:
array(1) {
[0]=>
string(0) ""
}
And not:
array(0) {
}If your data is smaller than the expected count with the list expansion:
<?php
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell,$nu) = explode(":", $data);
?>
The result is a warning not an error:
PHP Warning: Undefined array key 7 in ...
The solution is to pad the array to the expected length:
<?php
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell,$nu) = array_pad( explode(":", $data), 8, "");
// where 8 is the count of the list arguments
?>Be careful, while most non-alphanumeric data types as input strings return an array with an empty string when used with a valid separator, true returns an array with the string "1"!
var_dump(explode(',', null)); //array(1) { [0]=> string(0) "" }
var_dump(explode(',', false)); //array(1) { [0]=> string(0) "" }
var_dump(explode(',', true)); //array(1) { [0]=> string(1) "1" }If you want to directly take a specific value without having to store it in another variable, you can implement the following:
$status = 'Missing-1';
echo $status_only = explode('-', $status)[0];
// Missing