Jump to: navigation, search

Php explode

From w3cyberlearnings

Contents

PHP function explode

This function splits a string specifies by the delimiter.

Syntax explode

  • delimiter: specifies the string separator
  • string: string input
  • limit: maximum number of array allow for return
explode(delimiter,string,limit)

Example 1

<?php

$str = 'LeaderLLLManagerLLLCEOLLLProgrammer';
$role = explode('LLL', $str);
print_r($role);
?>


Output

Array
(
    [0] => Leader
    [1] => Manager
    [2] => CEO
    [3] => Programmer
)

Example 2

<?php

$arr = array(
    '10 15 20 30',
    'Mark Janny Lee Dim',
    'Group1 Group2 Group3 Grup4'
);

foreach($arr as $p) {
    $data = explode(' ',$p);
    print_r($data);
}

?>


Output

Array
(
    [0] => 10
    [1] => 15
    [2] => 20
    [3] => 30
)
Array
(
    [0] => Mark
    [1] => Janny
    [2] => Lee
    [3] => Dim
)
Array
(
    [0] => Group1
    [1] => Group2
    [2] => Group3
    [3] => Grup4
)

Example 3:Return only two arrays

<?php

$str = 'a:b:c:d';
$role = explode(':', $str,2);
print_r($role);
?>

Output

Array 
( 
   [0] => a 
   [1] => b:c:d 
)

Example 4: Return only 3 arrays

<?php

$str = 'a:b:c:d';
$role = explode(':', $str,3);
print_r($role);
?>

Output

<body>Array
(
    [0] => a
    [1] => b
    [2] => c:d
)
</body>

Related Functions


Navigation
Web
SQL
MISC
References