PHP Velho Oeste 2024

explode

(PHP 4, PHP 5, PHP 7)

explode문자열을 문자열로 나눕니다

설명

array explode ( string $delimiter , string $string [, int $limit ] )

delimiter 문자열을 경계로 나누어진 string의 부분 문자열로 이루어지는 배열을 반환합니다.

인수

delimiter

경계 문자열.

string

입력 문자열.

limit

limit를 지정하면, 반환하는 배열은 최대 limit개의 원소를 가지고, 마지막 원소는 남은 string 모두를 포함합니다.

limit 인수가 음수이면, 마지막 -limit를 제외한 모든 구성요소를 반환합니다.

implode()는 관습으로 인해 인수의 순서를 바꿀 수 있지만, explode()는 바꿀 수 없습니다. 반드시 delimiter 인수가 string 인수 앞에 위치해야 합니다.

반환값

delimiter가 빈 문자열("")이면, explode()FALSE를 반환합니다. delimiterstring에 존재하지 않으면, explode()string을 포함하는 배열을 반환합니다.

변경점

버전 설명
5.1.0 음수 limit 지원 추가
4.0.1 limit 인수 추가

예제

Example #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// *

?>

Example #2 limit 인수 예제

<?php
$str 
'one|two|three|four';

// 양수 limit
print_r(explode('|'$str2));

// 음수 limit (PHP 5.1부터)
print_r(explode('|'$str, -1));
?>

위 예제의 출력:

Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

주의

Note: 이 함수는 바이너리 안전입니다.

참고

add a note add a note

User Contributed Notes 4 notes

up
24
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
12
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
0
marc
5 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
-2
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