PHP explode() Function – Complete Guide with Examples
What is explode() in PHP?
The PHP explode()
function is used to split a string into an array using a specified delimiter.
🔹 Use case: When you need to break a string into smaller parts, like splitting a CSV line into values.
🔹 Syntax:
array explode(string $separator, string $string, int $limit = PHP_INT_MAX)
- $separator → The character(s) used to split the string.
- $string → The input string to be split.
- $limit (optional) → Defines the maximum number of elements in the returned array.
💡 Note: If the separator is not found, explode()
returns an array with one element (the original string).
1. 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.
2. Using explode() with Different Delimiters
You can split strings using different delimiters, such as spaces, dots, pipes, etc.
<?php
$text = "PHP|JavaScript|Python|C++";
$languages = explode("|", $text);
print_r($languages);
?>
✅ Output:
Array (
[0] => PHP
[1] => JavaScript
[2] => Python
[3] => C++
)
🔹 The delimiter |
is used to separate values.
3. Limiting the Number of Elements
Using the $limit
parameter, we can control the number of elements in the output array.
<?php
$string = "one,two,three,four,five";
$array = explode(",", $string, 3);
print_r($array);
?>
✅ Output:
Array (
[0] => one
[1] => two
[2] => three,four,five
)
📌 How it works?
- The first two splits happen normally.
- The remaining part is kept as one single element.
4. Handling Extra Spaces in explode()
If there are spaces around the delimiter, explode()
keeps them in the output:
<?php
$string = "Apple, Banana , Cherry ,Date";
$array = explode(",", $string);
print_r($array);
?>
✅ Output:
Array (
[0] => Apple
[1] => Banana
[2] => Cherry
[3] => Date
)
🔹 Notice the spaces!
Fix: Trim Each Element After Splitting
<?php
$string = "Apple, Banana , Cherry ,Date";
$array = array_map('trim', explode(",", $string));
print_r($array);
?>
✅ Output (cleaned up):
Array (
[0] => Apple
[1] => Banana
[2] => Cherry
[3] => Date
)
✔️ array_map('trim', $array)
removes extra spaces from each element.
5. Exploding Multi-Line Strings
You can split text line by line using "\n"
(newline) as a delimiter:
<?php
$text = "Line 1\nLine 2\nLine 3";
$lines = explode("\n", $text);
print_r($lines);
?>
✅ Output:
Array (
[0] => Line 1 [1] => Line 2 [2] => Line 3 )
🔹 This is useful for reading file contents line by line.
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);
?>
✅ Output:
Array (
[0] => https:
[1] =>
[2] => example.com
[3] => products
[4] => item?id=123 )
✔️ Now we can easily extract the domain, paths, or parameters!
7. implode() – The Opposite of explode()
To join an array back into a string, use implode()
:
<?php
$array = ["PHP", "JavaScript", "Python"];
$string = implode(" | ", $array);
echo $string;
?>
✅ Output:
PHP | JavaScript | Python
🔹 Use explode()
to split and implode()
to join!
8. Common Mistakes & Solutions
❌ Forgetting to Check for an Empty String
<?php $string = ""; $array = explode(",", $string); print_r($array); ?>
✅ Fix:
Always check if the string is empty before exploding.
<?php if (!empty($string)) { $array = explode(",", $string);
} else { $array = [];
} ?>
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