PHP explode() function

The PHP explode() functions converts string to an array using a given separator. By using explode() function you can convert string to array providing separator. The explode function is binary-safe and you can pass the array elements in any order. But keep in mind the separator parameter should not be the empty string.

Basic Syntax

string explode(separator,array,limit)

where,

NameDescription
separatorThis is an optional parameter used to break the string and add it to the array.
arrayAn array of elements to convert to string.
limitOptional, this specifies the number of array elements to return

Return Value

The PHP explode function returns the string formed using array elements.

Examples

Following are the examples of explode() function.

1. Converting simple string to array using explode() function

The output should be:

Array ( [0] => Apple [1] => Banana [2] => Cat [3] => Dog [4] => Elephant )
Array ( [0] => Apple [1] => Banana )
Array ( [0] => Apple [1] => Banana [2] => Cat )

2. Converting comma separated string to array using explode() function

The output should be:

Array ( [0] => White [1] => Blue [2] => Red [3] => Orange [4] => Voilet [5] => black )

LEAVE A REPLY

Please enter your comment!
Please enter your name here