What is the purpose of explode in PHP?
The explode function is a PHP string handling function that splits a string into an array based on a specified delimiter.
Function Declaration:
array explode(string $delimiter, string $string [, int $limit = PHP_INT_MAX])
Explanation of parameters:
- delimiter: specifies the separator where the string will be split.
- string: the string to be split.
- Optional parameter, specifying the maximum number of times to split.
Return value:
- Return an array containing the split strings.
The apple fell from the tree.
The apple dropped down from the tree.
$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);
Output result:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
In the example above, we split the string $str by commas and obtained an array containing the split strings.