explode
Splits a string into an array on a given separator, with an optional limit on the number of pieces.
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$separator | string | The boundary string. It must not be empty. |
$string | string | The input string to split. |
$limitoptional | int= PHP_INT_MAX | With a positive limit the array holds at most that many elements, the last containing the rest of the string. A negative limit drops that many elements from the end. Zero is treated as 1. |
Return value
arrayAn array of strings created by splitting the input on the separator; an array containing the whole string when the separator is not found.
Errors
Throws a ValueError if separator is an empty string.
Changelog
- PHP 8.0.0Throws a ValueError when separator is given an empty string; previously it returned false.
explode is the workhorse for turning a delimited string into an array. It matches the separator literally — no regular expressions, no character classes — which makes it both fast and predictable.
When to reach for it
Use it for any single, fixed delimiter: splitting a path on /, a header on : , a tag list on ,, or a line on \t. If you need a pattern, alternatives or trimming built in, use preg_split instead; for CSV with quoted fields use str_getcsv, because explode will happily split inside quotes.
What trips people up
Splitting an empty string returns [""], an array of one empty element, not an empty array. Code that does count(explode(",", $tags)) to count tags reports 1 for no tags at all. Guard with $s === "" ? [] : explode(...).
Consecutive separators produce empty elements: explode(",", "a,,b") gives three items with an empty middle. Filter with array_filter($parts, 'strlen') if that is unwanted.
Since PHP 8.0 an empty separator throws a ValueError rather than returning false; to split into single characters use str_split or mb_str_split.
Example
When to use it
- Split a comma-separated tag or ID list submitted from a form into an array.
- Break a URL path or namespace into its segments on / or backslash.
- Split a header or config line into key and value with a limit of 2 so the value keeps any further separators.
Frequently asked questions
What is the difference between explode and preg_split?
explode splits on one literal string, while preg_split splits on a regular expression and can handle alternatives, character classes and limits on whitespace. explode is faster when a fixed delimiter is all you need.
Why does explode return an array with one empty string?
Because the input string was empty and the separator was never found, so the whole (empty) string is returned as the single element. Check for an empty input before splitting if you need an empty array.
How do I split a string into individual characters?
Use str_split for bytes or mb_str_split for multi-byte characters. Passing an empty separator to explode throws a ValueError in PHP 8.
More examples
Using limit to keep the rest intact
A limit of 2 stops after the first colon, so the value keeps its own colons and equals signs. Without the limit the same header splits into three pieces and the value is broken apart.
<?php
$header = "X-Trace: id=7; region=eu-west-1";
[$name, $value] = explode(":", $header, 2);
echo "name = ", $name, "\n";
echo "value = ", trim($value), "\n";
print_r(explode(":", $header));Empty pieces and empty input
The raw split yields four elements, two of them empty. Filtering on strlen removes them, and array_values renumbers the keys. The last two lines show the empty-input trap: a naive count says 1, the filtered count says 0.
<?php
$messy = "a,,b,";
print_r(explode(",", $messy));
print_r(array_values(array_filter(explode(",", $messy), "strlen")));
echo count(explode(",", "")), "\n";
echo count(array_filter(explode(",", ""), "strlen")), "\n";An empty separator throws
Since PHP 8.0 an empty separator raises a ValueError instead of returning false, so the failure is loud rather than silent. str_split is the correct way to break a string into single characters.
<?php
try {
explode("", "abc");
} catch (ValueError $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
print_r(str_split("abc"));
Discussion