PHP explode() Function

Learn via video courses
Topics Covered

Overview

In PHP, "explode" is a built-in function that allows you to split a string into an array of substrings based on a specified delimiter. The delimiter can be any character or sequence of characters, such as a comma, space, or a period. The function takes two arguments: the delimiter and the string to be exploded. When executed, the function returns an array of substrings that were separated by the specified delimiter. This function is commonly used when working with data that is stored as a string, such as a list of names, email addresses, or URLs.

Syntax of explode() in PHP

In PHP, explode is a built-in function that allows you to split a string into an array, based on a specified delimiter. Here's the syntax for explode:

Let's break down each part of the syntax:

  • delimiter: The string used to separate the input string into array elements.
  • string: The input string is to be split.
  • limit: The maximum number of elements to return in the array. If the limit is set to a positive integer, it will return an array with a maximum of limit elements. If the limit is set to a negative integer, it will return an array with all but the last limit elements.

Parameters of explode() in PHP

In PHP, the explode function allows you to split a string into an array, based on a specified delimiter. The function takes in two required parameters and an optional third parameter. Here are the parameter values of exploding in PHP:

  • $delimiter: This is a required parameter that specifies the string used to separate the input string into array elements. The delimiter can be any character or string, such as a comma, space, or hyphen.
  • $string: This is also a required parameter that specifies the input string to be split. The string can be any combination of characters, numbers, and symbols.
  • $limit: This is an optional parameter that specifies the maximum number of elements to return in the array. If this parameter is omitted, it will return all possible elements.

Return Type of explode() in PHP

The explode() method in PHP returns an array. The substrings that are produced when the original string is divided by the designated delimiter are the elements of this array.

The return type of explode() is always an array, even if the input string is empty or the delimiter is not found. If the delimiter is not found in the input string, the resulting array will contain a single element, which is the entire input string.

Here's an example:

Output

Explanation In this example, we have a string of fruit names separated by commas. By calling explode() with a comma as the delimiter, we split the string into an array of fruit names.

Working of explode() in PHP

The explode() function in PHP is used to split a string into an array, based on a specified delimiter. Here's how it works:

  • You provide a string that you want to split into an array, along with the delimiter that you want to use to split the string.
  • The explode() function scans through the string and looks for occurrences of the delimiter.
  • Each time the delimiter is found, explode() creates a new element in the resulting array and stores the characters that came before the delimiter in that element.
  • Once the entire string has been scanned, explode() returns the resulting array, with each element corresponding to a substring that was separated by the delimiter.

Here's an example to illustrate how explode() works:

Output

Explanation In this example, we have a string of fruit names separated by commas. We call explode() with a comma as the delimiter, which tells PHP to split the string into an array of substrings using commas as the separator. Explode () is a very useful function in PHP, as it allows you to easily split strings into arrays. You can use it to parse CSV files, split URLs into their parts, and much more.

More Examples

1. Split a string by a space delimiter:

Output

Explanation In this example, we use the explode() function to split a string into an array, using a space as the delimiter. The resulting array contains two elements, "Hello" and "world."

2. Split a string by a newline delimiter:

Output

Explanation In this example, we split a string into an array using a newline as the delimiter. The resulting array contains three elements, which correspond to the three lines of text in the original string.

3. Split a string by a comma and a space delimiter:

Output

Explanation In this example, we use the explode() function to split a string of fruit names into an array, using a comma and a space as the delimiter. The resulting array contains three elements, which correspond to the individual fruit names.

4. Split a string by a multi-character delimiter:

Output

Explanation In this example, we split a URL into its parts using "://" as the delimiter. The resulting array contains two elements, the protocol (http) and the rest of the URL.

5. Split a string into a fixed number of elements:

Output

Explanation In this example, we split a string of numbers into an array, but we specify that we only want to split the string into a maximum of three elements. The resulting array contains the first two numbers as the first two elements and the remaining part of the string as the third element.

Conclusion

  • The explode() function is used to split a string into an array based on a specified delimiter.
  • The delimiter can be a single-character or a multi-character string.
  • The function returns an array containing the substrings of the original string that were separated by the delimiter.
  • The array keys are numeric, starting from 0.
  • If the delimiter is not found in the string, the function returns an array containing the original string as its only element.
  • If the string is empty, the function returns an empty array.
  • The function is case-sensitive by default, but you can make it case-insensitive by using the str_ireplace() function instead.