CakeFest 2024: The Official CakePHP Conference

explode

(PHP 4, PHP 5, PHP 7, PHP 8)

explode文字列を文字列により分割する

説明

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

文字列の配列を返します。この配列の各要素は、 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]; // piece1
echo $pieces[1]; // piece2

// 例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list(
$user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo
$user; // foo
echo $pass; // *

?>

例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
)

注意

注意: この関数はバイナリデータに対応しています。

参考

  • preg_split() - 正規表現で文字列を分割する
  • str_split() - 文字列を配列に変換する
  • mb_split() - マルチバイト文字列を正規表現により分割する
  • str_word_count() - 文字列に使用されている単語についての情報を返す
  • strtok() - 文字列をトークンに分割する
  • implode() - 配列要素を文字列により連結する

add a note

User Contributed Notes 4 notes

up
26
Gerben
2 years ago
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) {
}
up
3
marc
4 months ago
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
?>
up
13
bocoroth
3 years ago
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" }
up
-1
Alejandro-Ihuit
1 year ago
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
To Top