PHP explode() function

The explode() function used to break an string into array

Syntax

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

Parameters

Parameter Name Descrption
separator Required, specifies separator to break the string
string Required, string
limit Optional, Set the limit for the return number of elements
  • 0 - if limit is zero, then returns an array with one element
  • Less than 0 - return an array except the last elements
  • Greater than 0 - return an array will contain limit elements, and last element contain remaining of string

Return Value

Returns the array

PHP explode() examples

$string = 'This is test string';
$arary = explode(' ',$string);
print_r($arary);
Array
(
    [0] => This
    [1] => is
    [2] => test
    [3] => string
)

$arary = explode(' ',$string,0);
print_r($arary);
Array
(
    [0] => This is test string
)

$arary = explode(' ',$string,2);
print_r($arary);
Array
(
    [0] => This
    [1] => is test string
)

$arary = explode(' ',$string,-2);
print_r($arary);
Array
(
    [0] => This
    [1] => is
)

Related Functions

  • implode(): Join array elements with a string