How to Split a String in PHP with explode
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
$<pieces> = explode(<separator>, <text>);
$<pieces> = explode(<separator>, <text>, <limit>);
| Part | What 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:

<?php
$date = '2026-11-28';
print_r(explode('-', $date));
print_r(explode('-', $date, 2));
print_r(explode('/', $date));
Step 2: Run It
php main.php
Array
(
[0] => 2026
[1] => 11
[2] => 28
)
Array
(
[0] => 2026
[1] => 11-28
)
Array
(
[0] => 2026-11-28
)

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
/, soexplodereturned an array whose only element is the whole string.
Common Mistakes
| Mistake | What happens | Do 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 separator | ValueError: explode(): Argument #1 ($separator) must not be empty, use str_split() to split a string into characters | Use str_split($text) for single characters |
| Expecting empty pieces to disappear | explode(',', 'a,b,,c') keeps an empty string between the commas | Filter them with array_filter if you don't want them |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| An array with one element, the whole string | The separator doesn't occur in the string | Check the separator |
Warning: Undefined array key 1 after a split | Fewer pieces than the code expected | Check 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.
