Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

explode

Splits a string into an array by a separator.

Available since: PHP 4

Syntax

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

Parameters

ParameterTypeDescription
$separatorstringThe separator to split by.
$stringstringThe source string.
$limitintThe maximum number of elements. The last element receives the rest of the string.

Return value

arrayAn array of the string's parts.

Examples

Splitting by a comma:

<?php
 
print_r(explode(',', 'red,green,blue'));

Output:

Array
(
    [0] => red
    [1] => green
    [2] => blue
)

Limiting the number of parts:

<?php
 
print_r(explode(' ', 'one two three four', 2));

Output:

Array
(
    [0] => one
    [1] => two three four
)

Notes

An empty separator throws an error. To split a string into single characters, use mb_str_split().

See also

  • implode — Joins array elements into a string.

Related recipes

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.