Home » PHP explode() Function – Split Strings into Arrays (With Examples)

PHP explode() Function – Complete Guide with Examples

The PHP explode() function is one of the most useful string functions. It allows you to split a string into an array based on a specific separator (delimiter).

If you’re working with CSV data, user input, or text parsing, this function is essential.

In this guide, you’ll learn everything about explode() with simple explanations, examples, and real-world use cases.

What is explode() in PHP?

The explode() function splits a string into multiple parts using a delimiter and returns an array.

Simple idea:
Break a string → into pieces → using a separator

Syntax of explode()

array explode(string $separator, string $string, int $limit = PHP_INT_MAX)

Parameters Explained

ParameterDescription
$separatorThe character used to split the string
$stringThe input string
$limitOptional. Limits number of elements

Return Value

  • Returns an array of strings
  • Each element is a part of the original string

Basic Example of explode()

Let’s split a comma-separated string into an array:

<?php
$string = "Apple,Banana,Cherry,Date";
$array = explode(",", $string);
print_r($array);
?>

Output:

Array
(
[0] => Apple
[1] => Banana
[2] => Cherry
[3] => Date
)

 The delimiter , is used to separate the values.

Explanation

  • Comma , is the separator
  • The string is split into 4 parts
  • Result is stored in an array

This is the most common use of explode()

Example with Limit Parameter

<?php
$text = "one-two-three-four";
$result = explode("-", $text, 2);

print_r($result);
?>

Output

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

Explanation

  • Limit = 2 → only 2 parts created
  • Remaining string stays together

Real-World Use Cases

Processing CSV Data 

<?php
$csv = "John,25,India";
$data = explode(",", $csv);

echo $data[0]; // John
?>

Splitting User Input

<?php
$tags = "php,html,css";
$tagArray = explode(",", $tags);
?>

Extracting File Name

<?php
$file = "image.jpg";
$parts = explode(".", $file);

echo $parts[0]; // image
?>

URL Parsing

<?php
$url = "www.example.com/page";
$parts = explode("/", $url);
?>

6. Real-World Example: Splitting a URL into Parts

Let’s break down a URL into its components:

<?php
$url = "https://example.com/products/item?id=123";
$parts = explode("/", $url);
print_r($parts);
?>

Common Mistakes to Avoid

Using Wrong Separator

Wrong: explode(“;”, “a,b,c”); ✔ Correct:

explode(“,”, “a,b,c”);

Empty Separator

explode(“”, “text”); // Warning

Separator must not be empty.

Accessing Non-Existing Index

echo $result[5]; // Error
✔ Always check:
if (isset($result[5])) {
echo $result[5];
}

When to Use explode()

Use explode() when:

  • Splitting comma-separated values (CSV)
  •  Extracting words from a sentence
  •  Breaking down URLs into components
  •  Handling multi-line file content

Remember:
 An empty string returns an array with one empty element
 Spaces are not automatically removed – use trim() if needed

Best Practices

  • Always validate input string
  • Use correct delimiter
  • Handle missing values safely
  • Combine with implode() for reverse operation

explode() vs implode()

FunctionPurpose
explode()String → Array
implode()Array → String

Advanced Example (Full Dem

<?php
$data = "name:John|age:25|country:India";
$items = explode("|", $data);

foreach ($items as $item) {
    $pair = explode(":", $item);
    echo $pair[0] . " = " . $pair[1] . "<br>";
}
?>

Output

name = John
age = 25
country = India

Explanation

  • First split by |
  • Then split key-value using :
  • Useful for structured data parsing

FAQs

What does explode() do in PHP?

It splits a string into an array using a delimiter.

 What is delimiter in explode()?

A character used to break the string (e.g., comma, space).

 Can explode() work with spaces?

Yes:

 
explode(” “, “Hello World”);
 

What happens if delimiter is not found?

The entire string becomes a single array element.

 What is the opposite of explode()?

implode() is the reverse function.

Scroll to Top