PHP explode() Function
PHP explode() Syntax
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
PHP explode() Basic examples
$text = "apple,banana,orange";
$parts = explode(",", $text);
var_dump($parts);
array(3) {
[0]=> string(5) "apple"
[1]=> string(6) "banana"
[2]=> string(6) "orange"
}
Splits a comma-separated string into an array.
$text = "one|two|three";
$parts = explode("|", $text, 2);
var_dump($parts);
array(2) {
[0]=> string(3) "one"
[1]=> string(9) "two|three"
}
Limits the number of elements in the resulting array.
$text = "hello world";
$parts = explode(",", $text);
var_dump($parts);
array(1) {
[0]=> string(11) "hello world"
}
If the delimiter is not found, explode() returns an array with the original string.
PHP explode() Real-world usage
$csv = "id,name,email";
$columns = explode(",", $csv);
var_dump($columns);
array(3) {
[0]=> string(2) "id"
[1]=> string(4) "name"
[2]=> string(5) "email"
}
Parses a CSV header line into column names.
$path = "/var/www/html";
$parts = explode("/", trim($path, "/"));
var_dump($parts);
array(3) {
[0]=> string(3) "var"
[1]=> string(3) "www"
[2]=> string(4) "html"
}
Splits a filesystem path into directories.
PHP explode() Edge cases
$result = explode(",", "");
var_dump($result);
array(1) {
[0]=> string(0) ""
}
Exploding an empty string returns an array with one empty element.
$result = explode("", "abc");
Warning: explode(): Empty delimiter
Using an empty delimiter triggers a warning.
PHP explode() Common mistakes
Assuming explode trims whitespace
explode() does not remove whitespace from elements.
$parts = explode(",", "a, b, c");
var_dump($parts);
$parts = array_map('trim', explode(",", "a, b, c"));
var_dump($parts);
Use trim() on each element after exploding.
Using explode when preg_split is required
explode() only works with fixed delimiters.
$parts = explode(",", "a,,b");
$parts = preg_split('/,+/', "a,,b");
Use preg_split() for pattern-based splitting.
PHP explode() Frequently Asked Questions
What does explode() do in PHP?
explode() splits a string into an array using a specified delimiter.
What is the syntax of explode()?
explode(string $delimiter, string $string, int $limit = PHP_INT_MAX): array
What happens if the delimiter is not found?
explode() returns an array with the original string as the only element.
What is the limit parameter in explode()?
The limit parameter controls the number of elements in the returned array and how the string is split.
Can explode() return an empty array?
No, explode() always returns at least one element, even if the delimiter is not found.
What happens with an empty delimiter in explode()?
An empty delimiter will cause a warning or error depending on the PHP version.
What is the difference between explode() and str_split()?
explode() splits by delimiter, while str_split() splits a string into equal-sized chunks.
Can explode() handle multiple delimiters?
No, explode() supports only one delimiter. Use preg_split() for multiple delimiters.
Does explode() modify the original string?
No, explode() returns a new array and does not modify the original string.
Is explode() case-sensitive?
Yes, the delimiter matching in explode() is case-sensitive.
What is a real-world use case of explode()?
explode() is commonly used to parse CSV data, split URLs, or process user input.
How to combine explode() with trim()?
Use array_map("trim", explode(",", $string)) to split and clean each element.