Skip to main content

How to Split a String in PHP with explode

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

explode cuts a string into pieces wherever a separator appears and returns the pieces as an array. explode('-', '2026-11-28') gives the year, the month and the day. Note the order: the separator comes first.

Syntax​

Syntax
$<pieces> = explode(<separator>, <text>);
$<pieces> = explode(<separator>, <text>, <limit>);
PartWhat it does
<separator>The text to cut at. It must not be empty.
<text>The string to cut.
<limit>Optional. A positive limit returns at most that many pieces, the last one holding the rest; a negative one drops that many pieces from the end.

The PHP manual for explode: it "Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator."

The Example​

The example splits a date at its dashes, splits it again into at most two pieces, and splits it at a separator it doesn't contain. print_r prints each array with its indexes.

Prerequisites​

  • PHP for the command line. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which prints with print_r the result of explode at the dash on 2026-11-28, the same with a limit of 2, and explode at a slash.

main.php
<?php

$date = '2026-11-28';

print_r(explode('-', $date));
print_r(explode('-', $date, 2));
print_r(explode('/', $date));

Step 2: Run It​

Terminal
php main.php
Output
Array
(
[0] => 2026
[1] => 11
[2] => 28
)
Array
(
[0] => 2026
[1] => 11-28
)
Array
(
[0] => 2026-11-28
)

A zsh terminal where php main.php prints three arrays: 2026, 11 and 28; 2026 and 11-28; and the whole date as the only element.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • explode('-', $date) cut the date at both dashes into elements 0, 1 and 2.
  • With a limit of 2, the second element kept the rest of the date, 11-28.
  • The date has no /, so explode returned an array whose only element is the whole string.

Common Mistakes​

MistakeWhat happensDo this instead
Swapping the arguments, as in explode($date, '-')It splits '-' at the date, which never appears, and you get ['-']Separator first, then the string
Passing an empty separatorValueError: explode(): Argument #1 ($separator) must not be empty, use str_split() to split a string into charactersUse str_split($text) for single characters
Expecting empty pieces to disappearexplode(',', 'a,b,,c') keeps an empty string between the commasFilter them with array_filter if you don't want them

Troubleshooting​

SymptomLikely causeFix
An array with one element, the whole stringThe separator doesn't occur in the stringCheck the separator
Warning: Undefined array key 1 after a splitFewer pieces than the code expectedCheck count($pieces) first

FAQ​

How do I split into single characters?​

Use str_split('abc'), which returns ['a', 'b', 'c']; explode refuses an empty separator.

How do I join the pieces again?​

With implode: implode('-', $pieces) turns the array back into 2026-11-28.

Conclusion​

explode($separator, $text) returns the pieces between the separators as an array, with the separator first. A limit keeps the rest together in the last piece, and a missing separator gives you the whole string back as one element.

Additional Resources​