Description

The explode() function converts a string into an array by splitting it at a specified delimiter. It is one of the most commonly used string functions in PHP for parsing and processing text data.

Variables

The function accepts the following parameters:

  • $delimiter — the boundary string on which to split.
  • $string — the input string to be split.
  • $limit (optional) — controls the maximum number of array elements returned.

Return Values

explode() returns an array of strings created by splitting the $string parameter on boundaries formed by the $delimiter. If $delimiter is an empty string, the function returns false.

Examples

Example without using $limit

<?php
$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);
?>

/* Output:
Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
*/

Example using $limit

<?php
$str = "one|two|three|four";
$arr = explode("|", $str, 3);
print_r($arr);
?>

/* Output:
Array
(
    [0] => one
    [1] => two
    [2] => three|four
)
*/

When a positive $limit is provided, the returned array contains at most that many elements, with the final element containing the remainder of the string.

Originally published on freetuts.net.