PHP - explode() メソッド
関数 explode() は、文字列を配列に分割し、文字列の配列を返します。この関数は PHP バージョン 4+.メソッド explode() は、文字列を delimiter各配列は部分文字列です。
Notes:
Le separator パラメータを空の文字列にすることはできません.
The function explode() はブール型を返します.
Example
$str = "Hello world!";Runtime:
print_r (explode(" ",$str));
Array
(
[0] =>Hello
[1] =>world
[2] => !
)
Syntax
explode(separator,string,limit)
separator: 必須です。文字列を分割する区切り文字を指定します。
string: 必須。split.
limit: 省略可能な文字列。返す配列の数を指定します。
設定可能な値:
- 0より大きい - 最大要素数の配列を返します。
- 0未満: 最後の要素を除く配列を返します。
- 0 - 1 つの要素を持つ配列を返します。
例:
$str = 'オレンジ、青、緑、赤';Runtime:
// 制限 0
print_r(explode(',',$str,0));
// 0 の上限
print_r(explode(',',$str,2));
// 下限を 0 に
print_r(explode(',',$str,-1));
Array
(
[0] => オレンジ、青、緑、赤
)
Array
(
[0] => orange
[1] => blue, green, red
)
Array
(
[0] => orange
[1] => blue
[2] => green
)
References:
https://www.php.net/manual/fr/function.explode.php