PHP Programing language

explode() Functions in PHP
Previous Home Next
adplus-dvertising

explode()

The explode() function breaks a string into an array.

Syntax:
explode(separator,string,limit)

In above syntax, "separator" specifies where to break the string, "string" string to split and "limit" specifies the number of array elements to return. Possible values limits are:

  1. > 0 : Returns an array with a maximum of limit element(s)
  2. < 0 : Returns an array except for the last-limit elements()
  3. 0 : Returns an array with one element

Example:

<?php
$str = 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday';

// zero limit
print_r(explode(',',$str,0));
print "<br>";

// positive limit
print_r(explode(',',$str,4));
print "<br>";

// negative limit 
print_r(explode(',',$str,-2));
?>    

Output:

Array ( [0] => Monday,Tuesday,Wednesday, Thursday,Friday,Saturday, Sunday ) 
Array ( [0] => Monday [1] => Tuesday [2] => Wednesday [3] => Thursday,Friday,Saturday, Sunday ) 
Array ( [0] => Monday [1] => Tuesday [2] => Wednesday [3] => Thursday [4] => Friday )

Previous Home Next