Skip to content

[Edit] PHP: explode() #7068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 120 additions & 37 deletions content/php/concepts/string-functions/terms/explode/explode.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,164 @@
---
Title: 'explode()'
Description: 'Splits a given string by a delimiter and returns an array of the substrings produced.'
Description: 'Splits a string into an array based on a specified delimiter'
Subjects:
- 'Computer Science'
- 'Web Design'
- 'Web Development'
Tags:
- 'Arrays'
- 'Strings'
- 'Functions'
- 'Strings'
CatalogContent:
- 'learn-php'
- 'paths/computer-science'
---

The **`explode()`** function takes a string and splits it on a given delimiter string. It returns an array of the substrings produced.
The **`explode()`** function is a built-in PHP string function that splits a string into an array based on a specified delimiter. It takes a string and breaks it apart at every occurrence of the delimiter, returning an array containing the resulting substrings. This function is essential for string manipulation and data processing tasks in PHP applications.

The `explode()` function is commonly used in scenarios such as parsing CSV data, processing user input, splitting file paths, extracting words from sentences, and converting delimited strings into manageable array elements. It serves as the counterpart to the [`implode()`](https://www.codecademy.com/resources/docs/php/string-functions/implode) function, which joins array elements into a single string.

## Syntax

```pseudo
explode($delimiter, $string, $limit)
explode(separator, string, limit)
```

The `explode()` function takes three parameters:
**Parameters:**

- `separator`: The delimiter string that specifies where to split the original string. This parameter is required and cannot be an empty string.
- `string`: The input string to be split into an array. This parameter is required.
- `limit`: An optional integer parameter that controls the number of array elements to return. Possible values:
- **Positive integer**: Returns an array with a maximum of `limit` elements, with the last element containing the rest of the string
- **Negative integer**: Returns an array with all elements except for the last `limit` elements
- **Zero**: Returns an array containing one element (the whole string)
- **Not specified**: Returns an array with all possible elements

- `$delimiter`: a required `string` that specifies the characters on which `$string` will be split. The `$delimiter` has the following behaviors:
**Return value:**

- If `$delimiter` is an empty `string`, `explode()` will throw a `ValueError` exception.
- If `$delimiter` is not found in `$string` and a negative `$length` is used, an empty `array` is returned.
- If `$delimiter` is not found in `$string` and `$length` is positive or omitted, an `array` containing `$string` is returned.
- If `$delimiter` is found at the start or end of `$string`, empty values for each `$delimiter` will be added to the beginning or end of the returned `array` respectively.
The `explode()` function returns an array of strings, where each element is a substring of the original string separated by the specified delimiter.

- `$string`: a required `string` that will be split by the `explode()` function.
- `$limit`: an optional `int` that specifies how many `strings` will be returned in the resulting `array` as follows:
## Example 1: Basic String Splitting using `.explode()`

- A positive value will return up to `$limit` `strings` if more than `$limit` `strings` can be produced, splitting on `$delimiter`; the last element will contain the remainder of `$string`.
- A `$limit` of `0` is treated as `1`.
- A negative `$limit` will omit the last `n` elements from the result.
- If `$limit` is omitted, `explode()` will return as many substrings as possible.
This example demonstrates the fundamental usage of `explode()` to split a simple sentence into individual words:

```php
<?php
// Split a sentence into words using space as delimiter
$sentence = "Hello world from PHP programming";
$words = explode(" ", $sentence);

## Example
// Display the resulting array
print_r($words);
?>
```

The following example uses the `explode()` function to convert a `string` of fruits separated by `", "` to an `array` of fruits. Then the `print_r()` function prints the information about the returned `array` to the console:
The output of this code is:

```shell
Array
(
[0] => Hello
[1] => world
[2] => from
[3] => PHP
[4] => programming
)
```

This code splits the sentence at each space character, creating an array where each word becomes a separate element. This is useful for text processing and word analysis tasks.

## Example 2: Processing CSV Data

This example shows how `explode()` can be used to parse comma-separated values, which is common when working with data imports or API responses:

```php
<?php
print_r (explode(", ", "apple, strawberry, orange"));
// Simulate CSV data from a file or API
$csvData = "John,25,Engineer,New York";
$userInfo = explode(",", $csvData);

// Extract individual fields
$name = $userInfo[0];
$age = $userInfo[1];
$profession = $userInfo[2];
$city = $userInfo[3];

// Display parsed information
echo "Name: " . $name . "\n";
echo "Age: " . $age . "\n";
echo "Profession: " . $profession . "\n";
echo "City: " . $city . "\n";

// Alternative: Display the complete array
print_r($userInfo);
?>
```

The example will result in the following output:
This code produces the following output:

```shell
Name: John
Age: 25
Profession: Engineer
City: New York
Array
(
[0] => apple
[1] => strawberry
[2] => orange
[0] => John
[1] => 25
[2] => Engineer
[3] => New York
)
```

## Codebyte Example
This example demonstrates practical data parsing where a single string containing user information is split into manageable components. The `explode()` function makes it easy to access individual data fields for further processing or database storage.

The following shows three examples of the `explode()` function operating on one comma-delimited `string`.
## Codebyte Example: Advanced Usage with Limit Parameter

This example illustrates the power of the limit parameter to control how many array elements are created, which is useful for processing partially structured data:

```codebyte/php
<?php
$str = "list,of,comma,delimited,strings,";
$strarr1 = explode(",", $str);
$strarr2 = explode(",", $str, 5);
$strarr3 = explode(",", $str, -2);

var_dump($strarr1);
echo("<br/>");
var_dump($strarr2);
echo("<br/>");
var_dump($strarr3);
echo("<br/>");
// URL path processing with different limit values
$urlPath = "/category/subcategory/product/details/reviews";

// Split with positive limit - get first 3 parts only
$limitedSplit = explode("/", $urlPath, 3);
echo "With limit 3:\n";
print_r($limitedSplit);

// Split with negative limit - exclude last 2 parts
$negativeLimitSplit = explode("/", $urlPath, -2);
echo "With limit -2:\n";
print_r($negativeLimitSplit);

// Split without limit - get all parts
$fullSplit = explode("/", $urlPath);
echo "Without limit:\n";
print_r($fullSplit);

// Practical example: Extract domain parts
$email = "[email protected]";
$emailParts = explode("@", $email, 2);
$username = $emailParts[0];
$domain = $emailParts[1];

echo "Username: " . $username . "\n";
echo "Domain: " . $domain . "\n";
?>
```

This example shows how the limit parameter provides fine-grained control over string splitting. A positive limit creates a maximum number of elements, a negative limit excludes elements from the end, and the practical email example shows how limiting can prevent over-splitting when you only need specific parts.

## Frequently Asked Questions

### 1. What is the difference between `implode()` and `explode()`?

The `explode()` function splits a string into an array using a delimiter, while `implode()` does the opposite by joining array elements into a single string using a specified separator. They are complementary functions for string and array conversion.

### 2. Can the separator parameter be an empty string?

No, the separator parameter cannot be an empty string. PHP will generate a warning if you attempt to use an empty separator. Use alternative string functions like `str_split()` if you need to split a string into individual characters.

### 3. What happens if the delimiter is not found in the string?

If the delimiter is not found in the string, `explode()` returns an array containing the original string as the single element. No splitting occurs, and the function doesn't generate an error.