It is a built-in function in PHP, we use it for breaking string values into an array. Utilizing the explode() method we will make an array from a string. In simple words The explode() function breaks a string into an array, this function is binary-safe it’s a property basically, which means it can process any string.
- In the above syntax, the explode() function is using a separator that will break the $string,
- $string will declare or give by the user.
- $limit is indicating how many values the method will return to the outcome array.
- The separator is used to decide where the string should break first.
- The second is String, that specific string which you want to break.
- the Last one is Limit, it’s optional.
Positive: If the value of the parameter is positive, it means this number of elements contained by the array after splitting the string according to the delimiter, assuming that the quantity of elements is more noteworthy than the given value, the main n-1elements stay all things considered. The last element contains the leftover string.
Negative: If the parameter value is negative, then the keep going n elements of the array will be eliminated, and the excess piece of the string will be returned as a substring in the array. If we pass zero the element will contain just those elements which would the whole string Return value returns an array of strings made by parting the string boundary on limits shaped by the separator. Assuming that separator is an unfilled string (” “), explode() will show a Value Error.
Now let’s do practice, this will be more clear to you.
$str="E X P L O D E"; $res = explode(" ", $str); print_r($res);
We have a string and we need to make each letter a component of an array and access it independently.
Array (  => E  => X  => P  => L  => O  => D  => E )
Explode() function using limit
$str = 'apple|orange|mango|grapes'; $res = explode("|", $str, 3); print_r($res);
This limit means that we have given a specific number after that it will break into an array.
Array (  => apple  => orange  => mango|grapes )
Explode() function using negative(-) limit
This negative limit removed the value of that index number from an array.
Explode() Function Empty String
$str = ""; $res = explode(",",$str); print_r($res);
We passed an empty string in $str
Array (  => ) // or sometimes nothing
In the event that you split the unfilled string, you get back the one-component array with 0 as the key and the vacant string for the value.
Difference Between Php Explode() & Php Split()
The fundamental contrast between the explode() and split() methods is parting an enormous string. Both methods are utilized to part a string. Nonetheless, the split() work is utilized to part a string utilizing a normal articulation, while the explode() function is utilized to part a string utilizing another string.
In this tutorial we learned about PHP explode() function, we learn what is PHP explode() function and why we use it. We learn about its syntax and parameters and give a hand on its practical examples. I hope You will have proper knowledge about the PHP explode() function and you will be able to solve complex programming problems for more PHP tutorials.