0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP explode関数

Last updated at Posted at 2025-02-18

説明

文字列をも文字列により分割し、文字列の配列を返す。

使い方

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

使用例

使用例①

<?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) ""
)

使用例②

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

limitが負の値-nの場合は最後のn個を除くすべての構成要素が返される。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?