PHP explode function


PHP explode function : It breaks the string into an array. It takes string as an input and returns the array.


Here is the PHP Syntax

explode(separator, string, limit)

separator : Separator the delimiter in string from where to break it in array such as break a string where whitespace found.
string : String As Input.
limit : Is optional which specifies how many elements to return.

There are following possible limits :
a. limit > 0 : Return an array with max possible limit.
b. limit = 0 : Return an array with one element all string parts will be in one element of array.
c. limit < 0 : Return the array except the last element of array.

PHP explode Function Example 1 : Limit = 2 (>0)

$str = "a b c d e";
$array = explode(" " , $str, 2);
print_r($array);

Try it »

Output Will Be :

PHP explode function

PHP explode() function

PHP explode Function Example 2 : Limit = 0

$str = "a b c d e";
$array = explode(" " , $str, 0);
print_r($array);

Try it »

Output of the above example will be :

PHP explode function

PHP explode() function

PHP explode Function Example 3 : Limit = -1 (<0)

$str = "a b c d e";
$array = explode(" " , $str, -1);
print_r($array);

Try it »

PHP explode function

PHP explode() function


Advertisements

Add Comment

📖 Read More