W3docs

explode()

The explode() function is used to split a string into an array of substrings. The syntax of the explode() function is as follows:

The explode() function splits a string into an array of substrings, breaking it apart wherever a chosen delimiter appears. It is the standard PHP way to turn delimited text — a CSV row, a date, a comma-separated tag list — into data you can loop over. It is the inverse of implode(), which joins an array back into a string.

This page covers the syntax, the optional $limit parameter (including negative values), the return value, common parsing patterns, and the gotchas that trip people up.

Syntax

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
ParameterRequiredDescription
$separatoryesThe boundary string to split on. It is matched literally, not as a regular expression.
$stringyesThe input string to split.
$limitnoControls how many pieces are returned (see below). Defaults to PHP_INT_MAX.

The function returns an array of strings. If $separator is an empty string (""), explode() throws a ValueError (in PHP 8+; in older versions it returned false). If the separator never appears in the string, the result is a one-element array containing the whole original string.

Basic example

php— editable, runs on the server

We define the delimiter ,, call explode(), and print_r() the resulting array. Each element is everything between two delimiters.

The output of this code will be:

Array
(
    [0] => Hello
    [1] => World!
)

Limiting the number of pieces

The optional $limit parameter changes how the string is broken up:

  • Positive limit — the array contains at most that many elements; the last element holds the unsplit remainder of the string.
  • Zero — treated as 1, so the whole string is returned as a single element.
  • Negative limit — all pieces are returned except the last |limit| of them.

Positive limit

php— editable, runs on the server

With a limit of 3, the string is split into at most three elements. Note that the final element keeps the rest of the string unsplit — three,four,five — rather than being discarded.

The output of this code will be:

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

This is handy when you only care about the first few fields, for example explode(' ', $logLine, 2) to separate a timestamp from the rest of a log message.

Negative limit

A negative limit drops elements from the end of the result:

<?php
$string = "one,two,three,four,five";
print_r(explode(",", $string, -2));
?>

The last two pieces (four and five) are removed:

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

Common parsing patterns

A typical real-world use is reading a delimited record and trimming each field:

<?php
$csvRow = "Jane , Doe ,  [email protected] ";
$fields = array_map('trim', explode(",", $csvRow));
print_r($fields);
?>

The output is clean, whitespace-free values:

Array
(
    [0] => Jane
    [1] => Doe
    [2] => [email protected]
)

explode() also pairs naturally with list() destructuring to pull named parts out of a string:

<?php
$date = "2026-06-21";
[$year, $month, $day] = explode("-", $date);
echo "Year: $year, Month: $month, Day: $day";
?>

Output:

Year: 2026, Month: 06, Day: 21

Gotchas

  • The separator is literal, not a regex. explode("-", ...) splits on a hyphen, never a pattern. If you need a regular expression (for example, splitting on any run of whitespace), use preg_split() instead.
  • You cannot split into individual characters with explode(). An empty separator raises a ValueError. To split a string into single characters, use str_split().
  • Multiple adjacent delimiters create empty elements. explode(",", "a,,b") returns ["a", "", "b"]. Filter them out with array_filter() if you don't want them.
  • No separator found? You still get a valid array — a single-element array holding the whole string — so you can safely index [0].
  • implode() / join() — the reverse: join an array into a string.
  • str_split() — split a string into fixed-length chunks (or characters).
  • preg_split() — split using a regular expression.
  • substr() — extract part of a string by position.
  • trim() — clean whitespace from each piece after exploding.

Practice

Practice
What is the functionality of the 'explode' function in PHP?
What is the functionality of the 'explode' function in PHP?
Was this page helpful?