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