PHP explode() Function

PHP explode() Function spilts a string into an array.

Syntax :

explode(separator,string,limit);

It returns array of string.

Note: The "separator" parameter cannot be an empty string.

Parameter Values,

PHP explode() Function
Parameter Description
separator     Required. It specifies where to split the string
string Required. The string to split
limit Optional. It specifies the number of array elements to return.

Possible values:

    Positive - if limit >0 then it returns an array with a maximum of limit element(s)
    Negative - if limit <0 then returns an array except for the last -limit elements()
    0 - if limit=0 it returns an array with one element

 

 

Example :

<?php
$arr=explode(" ","Learn php tutorials by aryatechno"); // split string into array by space
print_r($arr); // it returns array

echo "<br><br> Limit is positive for explode() function <br>";
$arr_positive=explode(" ","Learn php tutorials by aryatechno",2); // split string into array by space and limit is positive
print_r($arr_positive); // it returns array

echo "<br><br> Limit is negative for explode() function <br>";
$arr_negative=explode(" ","Learn php tutorials by aryatechno",-1); // split string into array by space and limit is negative
print_r($arr_negative); // it returns array

echo "<br><br> Limit is zero for explode() function <br>";
$arr_zero=explode(" ","Learn php tutorials by aryatechno",0); // split string into array by space and limit is zero
print_r($arr_zero); // it returns array
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

72131