PHP explode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, explode() splits a string into an array of pieces using a delimiter.

Delimiter and string are required. An optional limit controls how many pieces you get.

Syntax

Call explode with a delimiter. Start with this line.

PHP
$parts = explode(",", $csv);
CallWhat it does
explode(",", "a,b,c")Returns ["a", "b", "c"].
explode(",", $csv, 2)Returns at most 2 pieces.

An empty delimiter is not allowed. Use str_split() to split into characters.

Library declarations
PHP
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

Parameters

ParameterDescription
separatorRequired. The delimiter that marks each split.
stringRequired. The input string to split.
limitOptional. Maximum number of pieces to return.

Return Value

Return ValueDescription
arrayAn array of string pieces.

PHP Examples

Example 1 — Split a CSV Line

The everyday call: separator first, then the string.

PHP
<?php
$line = "apple,banana,cherry";
$parts = explode(",", $line);

print_r($parts);
echo $parts[1], PHP_EOL; // banana

How It Works

  1. Pick the delimiter. Here it is a comma.
  2. Call explode. Argument order is separator, then string.
  3. Get three pieces. Indexes 0, 1, and 2.
  4. Read by index. $parts[1] is "banana".

Example 2 — Use a Positive Limit

Cap how many array elements you get. The last element keeps the leftover text.

PHP
<?php
$path = "docs/php/string/explode.md";
$parts = explode("/", $path, 2);

print_r($parts);
/*
Array
(
    [0] => docs
    [1] => php/string/explode.md
)
*/

How It Works

  1. Set limit to 2. Only two elements are returned.
  2. First split only. The first "/" separates docs.
  3. Remainder stays together. Extra slashes stay inside index 1.
  4. Useful for “head / rest”. Grab a prefix without full path parsing.

Example 3 — Round-Trip with implode()

Split, tweak a piece, then join back into one string.

PHP
<?php
$tags = "php,html,css";
$parts = explode(",", $tags);
$parts[1] = "js";
$again = implode(",", $parts);

echo $again, PHP_EOL; // php,js,css

How It Works

  1. explode to edit. Arrays are easier to change than raw strings.
  2. Replace one item. Index 1 becomes "js".
  3. implode to rebuild. Same delimiter stitches the pieces.
  4. Inverse pair. This is the usual explode ↔ implode workflow.

Frequently Asked Questions

explode() splits a string into an array of substrings using a delimiter. Each time the delimiter appears, a new array element begins. The delimiter itself is not kept in the pieces.
array explode(string $separator, string $string, int $limit = PHP_INT_MAX). Separator comes first, then the string. The optional limit controls how many pieces you get.
You get a one-element array with the whole original string. Example: explode(',', 'hello') returns ['hello'].
A positive limit caps the number of elements—the last one holds any leftover text. A negative limit drops that many elements from the end. A limit of 0 is treated as 1.
No. Since PHP 8.0, an empty separator throws a ValueError. To split into characters, use str_split() instead.

Next: fprintf()

Write a formatted string to a file or stream.

fprintf() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful