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,
Name | Description |
---|---|
separator | This is an optional parameter used to break the string and add it to the array. |
array | An array of elements to convert to string. |
limit | Optional, 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
<?php $str = "Apple Banana Cat Dog Elephant"; echo explode(" ",$str)."<br>"; $str = "Apple Banana Cat Dog Elephant"; echo explode(" ",$str,2)."<br>"; $str = "Apple Banana Cat Dog Elephant"; echo explode(" ",$str,3)."<br>"; ?>
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 )
Array ( [0] => Apple [1] => Banana )
Array ( [0] => Apple [1] => Banana [2] => Cat )
2. Converting comma separated string to array using explode() function
<?php $str = "White,Blue,Red,Orange,Voilet,Black"; echo explode(",",$str)."<br>"; ?>
The output should be:
Array ( [0] => White [1] => Blue [2] => Red [3] => Orange [4] => Voilet [5] => black )
LATEST POSTS
-
Numpy Zeros np.zeros() in Python
-
C strcmp() function with example
-
numpy.argmin() in Python
-
How to Sort Pandas DataFrame with Examples
-
numpy.mean() in Python
-
C strcat() function with example
-
Bubble Sort in Java
-
numpy.ndarray.flatten() in Python
-
numpy.sum() in Python
-
C++ while loop with Examples
-
Python square root sqrt() method
-
numpy.loadtxt() in Python