In PHP, explode() splits a string into an array using a delimiter, and implode() joins the elements of an array into one string using a glue string. They are exact opposites, so implode(',', explode(',', $s)) gives back the original text. Together, php explode implode calls handle most everyday parsing, such as reading comma-separated values, building URLs and printing lists.
This guide covers both functions, their limit argument, edge cases with empty strings, and the related helpers str_split(), preg_split() and str_word_count().
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array cuts the string at every occurrence of the separator.
The separator must not be an empty string; since PHP 8.0 that throws a ValueError. A separator longer than one character works as a whole, for example explode(' - ', $title).
A positive limit returns at most that many elements, and the last element holds the rest of the string. A negative limit drops that many elements from the end.
A limit of 2 is the standard way to split a key from a value when the value itself may contain the separator. This also works well for splitting an email at the first @ or a header line at the first colon.
Splitting an empty string does not return an empty array. It returns an array with one empty string, which surprises many beginners.
If empty items are unwanted, remove them with array_filter(), and renumber the keys with array_values().
array_map('trim', ...) removes stray spaces, and array_filter() with no callback removes empty strings. Be careful: the same call would also remove the string "0", because it is falsy. Pass a callback such as fn($v) => $v !== '' if "0" is a valid value.
implode(string $separator, array $array): string joins the values with the separator between them. The call join() is an alias.
The separator-first form is the only one supported since PHP 8.0; the old reversed argument order was removed. Non-string values are converted to strings, true becomes 1, and null and false become empty strings. Nested arrays produce the text Array and a warning, so flatten them first.
A common task is joining items with commas and a final "and". There is no built-in function, but a few lines handle it.
explode() only handles one fixed separator. For other needs use these functions.
| Need | Function | Example |
|---|---|---|
| Split into chunks of N bytes | str_split($s, 3) | ['abc', 'def'] |
| Split on several separators | preg_split('/[,;\s]+/', $s) | commas, semicolons or spaces |
| Split text into words | preg_split('/\s+/', trim($s)) | ignores repeated spaces |
| Split UTF-8 into characters | mb_str_split($s) | one item per character |
explode() turns a string into an array, and implode() turns an array into a string. One needs a separator to cut on, the other a glue string to put between items.
An empty input has no separator in it, so the whole input (an empty string) becomes the single piece. Check with $s === '' first if you want an empty array instead.
Use str_split($s) for ASCII or mb_str_split($s) for UTF-8 text so multibyte characters are not cut in half.
Yes, but it joins only the values and ignores the keys. To include keys, build the pieces with array_map() or http_build_query() for query strings.
Use explode() to split on a fixed separator, with a limit of 2 to split off the first part. Use implode() with the separator first to join array values. Remember that an empty string splits into [''], clean the result with trim and array_filter, and move to preg_split() when the separators vary.