Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

explode

Description

The explode of String for PHP split a string by a string.

Syntax

explode(
    string $separator,
    string $string,
    int $limit = PHP_INT_MAX
): array

Parameters

separator

The boundary string.

string

The input string.

limit

If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

If the limit parameter is negative, all components except the last -limit are returned.

If the limit parameter is zero, then this is treated as 1.

NOTE: Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the separator argument comes before the string argument.

Return

Returns an array of strings created by splitting the string parameter on boundaries formed by the separator.

If separator is an empty string (""), explode() throws a ValueError. If separator contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned. If separator values appear at the start or end of string, said values will be added as an empty array value either in the first or last position of the returned array respectively.

Examples

1 · separator · comma

<?

$separator = ",";
$string = "word1,word2,word3,word4,word5";

$return = explode($separator, $string);

print_r($return);

?>
Array
(
    [0] => word1
    [1] => word2
    [2] => word3
    [3] => word4
    [4] => word5
)

2 · separator · newline

<?

$separator = "\n";
$string = "word1\nword2\nword3\nword4\nword5";

$return = explode($separator, $string);

print_r($return);

?>
Array
(
    [0] => word1
    [1] => word2
    [2] => word3
    [3] => word4
    [4] => word5
)

3 · separator · space

<?

$separator = " ";
$string = "word1 word2 word3 word4 word5";

$return = explode($separator, $string);

print_r($return);

?>
Array
(
    [0] => word1
    [1] => word2
    [2] => word3
    [3] => word4
    [4] => word5
)

4 · separator · empty

<?

$separator = "";
$string = "word1 word2 word3 word4 word5";

$return = explode($separator, $string);

print_r($return);

?>

5 · string · empty

<?

$separator = " ";
$string = "";

$return = explode($separator, $string);

print_r($return);

?>
Array
(
    [0] => 
)

6 · string · 1 separator

<?

$separator = " ";
$string = " ";

$return = explode($separator, $string);

print_r($return);

?>
Array
(
    [0] => 
    [1] => 
)

7 · string · 1 element

<?

$separator = " ";
$string = "word1";

$return = explode($separator, $string);

print_r($return);

?>
Array
(
    [0] => word1
)

8 · string · 1 element 1 separator

<?

$separator = " ";
$string = "word1 ";

$return = explode($separator, $string);

print_r($return);

?>
Array
(
    [0] => word1
    [1] => 
)

9 · limit · negative

<?

$separator = " ";
$string = "word1 word2 word3 word4 word5";
$limit = -3;

$return = explode($separator, $string, $limit);

print_r($return);

?>
Array
(
    [0] => word1
    [1] => word2
)

10 · limit · zero

<?

$separator = " ";
$string = "word1 word2 word3 word4 word5";
$limit = 0;

$return = explode($separator, $string, $limit);

print_r($return);

?>
Array
(
    [0] => word1 word2 word3 word4 word5
)

11 · limit · positive

<?

$separator = " ";
$string = "word1 word2 word3 word4 word5";
$limit = 3;

$return = explode($separator, $string, $limit);

print_r($return);

?>
Array
(
    [0] => word1
    [1] => word2
    [2] => word3 word4 word5
)

12 · limit · PHP_INT_MAX

<?

$separator = " ";
$string = "word1 word2 word3 word4 word5";
$limit = PHP_INT_MAX;

$return = explode($separator, $string, $limit);

print_r($return);

?>
Array
(
    [0] => word1
    [1] => word2
    [2] => word3
    [3] => word4
    [4] => word5
)

13 · array index

<?

$separator = " ";
$string = "word1 word2 word3 word4 word5";

$return = explode($separator, $string);

echo $return[0] . PHP_EOL;
echo $return[1] . PHP_EOL;
echo $return[2] . PHP_EOL;
echo $return[3] . PHP_EOL;
echo $return[4];

?>
word1
word2
word3
word4
word5

14 · list variable

<?

$separator = " ";
$string = "word1 word2 word3 word4 word5";

list($var1, $var2, $var3, $var4, $var5) = explode($separator, $string);

echo $var1 . PHP_EOL;
echo $var2 . PHP_EOL;
echo $var3 . PHP_EOL;
echo $var4 . PHP_EOL;
echo $var5;

?>
word1
word2
word3
word4
word5
HomeMenu