PHP explode() Function | What is explode() in PHP?

Summary: In this article, we demonstrate and explore PHP explode() function, and what is explode() in PHP and how to use it? Let’s understand in detail this PHP function with an example.

What is PHP explode() Function?

The explode() function is a built-in function in PHP that is used to split a string into different strings or “Split a string by a specified string into pieces i.e. it breaks a string into an array”. The explode() function in PHP splits a string based on a string delimiter, i.e. this function splits the string while the delimiter character occurs. This common built-in function returns an array containing the strings formed by splitting the original string.

.
Important Note: The “separator” parameter can’t be an empty string.
This function is binary-safe.

Syntax:

array explode(separator, OriginalString, NoOfElements)

Parameter Value:

The PHP explode function accepts only three parameters of which two are mandatory and one is optional. All three parameters are Mentioned below:-

Parameter Value Description
separator: This character specifies the critical points or points at which the string will split, i.e. whenever this character is found in the string it symbolizes the end of one element of the array and the start of another.
OriginalString: The input string that is to be split into the array.
NoOfElements: This is an optional parameter. It’s utilized to specify the number of elements of the array. This parameter may be any whole number ( positive, negative, or zero).

  • Positive (N): When this parameter is passed with a positive value it implies that the array will contain this number of elements. If the number of elements after separating or deductive with respect to the separator emerges to be greater than this value the 1st N-1 elements remain the same or equivalent and the last element is the whole remaining string.
  • Negative (N): If the negative value is passed as a parameter then the last N elements of the array will be trimmed out and the remaining part of the array shall be returned as a single array.
  • Zero: If this parameter is Zero then the array returned will have only one element i.e. the whole string.
.
Important Note: When this parameter is not provided the array returned contains the total number of elements formed after separating the string with the separator.

Return Type: The return type of PHP explode() function is array of strings.

PHP explode() Function Example

Let’s see an example, you have a string such as given below:-

now you wish to make every name as an element of an array and access it individually so what you do:

Means that we’ve made pieces of string $text based on separator ',' and put the resulting array in variable $myarr
So I used print_r ($myarr), and therefore the results are the following:

so, means that which is equal to:

Program 1:

Array (
[0] => Keep
[1] => your
[2] => friends
[3] => close,
[4] => but
[5] => your
[6] => enemies
[7] => closer.
)
Array (
[0] => Keep
[1] => your
[2] => friends
[3] => close,
[4] => but your enemies closer.
)
Array (
[0] => Keep
[1] => your
[2] => friends
[3] => close,
[4] => but
[5] => your
)
Program 2:

Array (
[0] => ram,shyam,krishna,radhe
)
Array (
[0] => ram
[1] => shyam,krishna,radhe
)
Array (
[0] => ram
[1] => shyam
[2] => krishna
)
Program 3: explode() Return Examples

array (size=1)
0 => string ‘welcome’ (length=7)
array (size=2)
0 => string ‘welcome’ (length=7)
1 => string ‘investor’ (length=7)
array (size=2)
0 => string ” (length=0)
1 => string ” (length=0)
Program 4: Limit Parameter Examples

Array
(
[0] => ram
[1] => shyam|krishna|radhe
)
Array
(
[0] => ram
[1] => shyam
[2] => krishna
)

Conclusion:

In this article, you have learned PHP explode() Function and explode() example.I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!