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