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 5 notes

up
5
Emilio Bravo
3 years ago
$string = "PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION";
$exploded = explode("::",$string);
/*

explode('::',$string) = eliminate every :: and for each division of ::make an array element

Example:

PDO::ERRMODE_EXCEPTION (exploded) = array     (
                                                    [0] => string PDO
                                                    [1] => string ERRMODE_EXCEPTION
                                               )
Example:

$exploded[0] = "PDO";
*/
foreach ($exploded as $index) {
    echo $index . "\n";
}
/*

Output:

PDO
ATTR_ERRMODE => PDO
ERRMODE_EXCEPTION

*/
up
2
henrik Schmidt
3 years ago
"Return value" text needs updating for php 8, an empty delimiter now throws an Exception.
up
4
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
-7
David Spector
3 years ago
When using 'explode' to create an array of strings from a user-specified string that contains newline characters, you may wish the resulting array to correctly reflect the user's intentions by ignoring any final empty line (many users like to end multi-line input with a final newline, for clarity).

Here is a function to call after 'explode' to support this effect:

// When using explode, delete the last line in the array of lines when it is empty
function IgnoreEmptyLastLine(&$linesArr)
    {
    $last=count($linesArr)-1;
    if ($last>=0 && !$linesArr[$last])
        unset($linesArr[$last]);
    } // IgnoreEmptyLastLine
up
-29
leandro at primersistemas dot com dot br
3 years ago
function aexplode($delimiters,$string,$trimduplicate = false) {
    if (!is_array($delimiters))
        return explode($delimiters,$string);
    $stringaux = str_replace($delimiters, $delimiters[0], $string);
    if ($trimduplicate)
        while (strpos($stringaux,$delimiters[0].$delimiters[0]) !== false)
            $stringaux = str_replace($delimiters[0].$delimiters[0],$delimiters[0],$stringaux);
    return explode($delimiters[0],$stringaux);
}

This functions will work and accept array.
To Top