explode($separator, $string) splits a string at every occurrence of $separator and returns the pieces as an array. explode(',', 'apple,banana,cherry') gives ['apple', 'banana', 'cherry'].
The separator can be any string, not just one character: explode(' and ', 'tea and milk and sugar') works the same way. The opposite operation, joining an array back into a string, is implode().
Syntax and return value
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
It always returns an array. If the separator does not appear, the array holds the whole string as its only element. That includes the empty string, which gives an array with one empty string in it, a common source of "my loop runs once on empty input" bugs.
Limit the number of pieces
The third argument, $limit, changes how many pieces you get:
- A positive limit returns at most that many pieces. The last piece holds the rest of the string, separators included.
- A negative limit returns every piece except the last
abs($limit)ones. - A limit of
0is treated as1.
A positive limit is the right tool when only the first separator matters, for example a key=value line where the value may contain = too: explode('=', 'query=a=b', 2) gives ['query', 'a=b']. The next sections use it that way.
To get only the last piece, store the array in a variable first: $parts = explode('/', $path); $file = end($parts);. Writing end(explode('/', $path)) still returns the right value, but PHP prints Notice: Only variables should be passed by reference, because end() takes its argument by reference.
Split a string by newline
explode("\n", $text) splits at line breaks. Use double quotes: in single quotes '\n' is a backslash followed by the letter n, not a newline. Text from Windows files or HTML forms often ends lines with "\r\n", which leaves a stray "\r" at the end of each piece. preg_split('/\R/', $text) splits at any kind of line break.
Trim the pieces and drop empty ones
User input like "red, green , ,blue" has spaces around the items and an empty item in the middle. Pass the pieces through trim() with array_map(), then remove empty strings with array_filter(). array_values() renumbers the keys afterwards, because array_filter() keeps the original ones.
The callback fn($c) => $c !== '' keeps the string "0". A bare array_filter($colors) would drop it, because "0" is falsy.
Turn a string into an associative array
Settings, cookies and query-like strings are often stored as key=value pairs. Explode twice: once for the pairs, once for each pair with a limit of 2, unpacking the two pieces into variables (array destructuring).
A pair with no = in it, such as debug, gives a one-element array, and the destructuring prints Warning: Undefined array key 1. Pad the result to two elements to avoid it: [$key, $value] = explode('=', $pair, 2) + [1 => ''];.
For a real URL query string, parse_str() does this for you and also decodes %20 and +.
Split by several delimiters
explode() takes one separator. To split at commas, semicolons or spaces, use preg_split() with a character class. PREG_SPLIT_NO_EMPTY drops the empty pieces that two delimiters in a row would create.
Japanese and other UTF-8 text
explode() compares bytes, and in UTF-8 a full character never appears inside another character's bytes, so splitting Japanese text at a Japanese separator such as 、 or a full-width comma works correctly.
explode vs str_split vs preg_split
| Function | Splits at | Example | Result |
|---|---|---|---|
explode() | a fixed separator string | explode('-', '2026-03-14') | ['2026', '03', '14'] |
str_split() | every N bytes | str_split('abcdef', 2) | ['ab', 'cd', 'ef'] |
mb_str_split() | every N characters | mb_str_split('日本語') | ['日', '本', '語'] |
preg_split() | a regular expression | preg_split('/\s+/', 'a b') | ['a', 'b'] |
explode() does no regex work, so it is faster than preg_split() and the one to reach for whenever the separator is a fixed string.
Common mistake: an empty separator
An empty separator is an error. Since PHP 8.0 explode('', 'abc') throws a ValueError instead of returning false, and the script stops:
PHP Fatal error: Uncaught ValueError: explode(): Argument #1 ($separator) cannot be empty in /home/index.php:3
To split a string into characters, use str_split('abc') for ASCII text or mb_str_split($s) for anything that may contain multibyte characters.
Frequently Asked Questions
What does explode() do in PHP?
It splits a string at every occurrence of a separator and returns the pieces as an array: explode(',', 'a,b,c') returns ['a', 'b', 'c']. The separator itself is not included in any piece.
What does explode() return if the separator is not found?
An array with one element, the whole string: explode(',', 'apple') returns ['apple']. An empty string gives [''], an array with one empty string, so count() returns 1, not 0.
How do I explode a string by multiple delimiters in PHP?
Use preg_split() with a character class: preg_split('/[,;|]/', $s) splits at commas, semicolons and pipes. To also drop empty pieces, pass a limit of -1 and the flag: preg_split('/[,;|]/', $s, -1, PREG_SPLIT_NO_EMPTY).
What is the difference between explode() and str_split()?
explode() splits at a separator you choose. str_split($s, 3) cuts the string into chunks of a fixed length, counted in bytes. For Japanese or other multibyte text use mb_str_split($s), which counts characters.
Why does explode() throw a ValueError?
Because the separator is an empty string. Since PHP 8.0, explode('', $s) throws ValueError: explode(): Argument #1 ($separator) cannot be empty. To split a string into single characters use str_split() or mb_str_split().