Open In App

PHP | Imploding and Exploding

Imploding and Exploding are couple of important functions of PHP that can be applied on strings or arrays. PHP provides us with two important builtin functions implode() and explode() to perform these operations. As the name suggests Implode/implode() method joins array elements with a string segment that works as a glue and similarly Explode/explode() method does the exact opposite i.e. given a string and a delimiter it creates an array of strings separating with the help of the delimiter.

implode() method



Syntax:

string implode (glue ,pieces)

or,



string implode (pieces)

Parameters:The function accepts two parameters as described below.

Return Type: This function traverses the input array and concatenates each element with one glue segment separating them to construct and return the final imploded string.

Below program illustrates the working of implode() in PHP:




<?php
// PHP code to illustrate the working of implode()
  
$array1 = array('www', 'geeksforgeeks', 'org');
echo(implode('.',$array1)."<br>"); 
  
$array2 = array('H', 'E', 'L', 'L', 'O');
echo(implode($array2));
?>

Output:

www.geeksforgeeks.org
HELLO

You may refer to the article on PHP | implode() function to learn about implode() in details.

explode() method

Syntax:

array explode (delimiter, string, limit)

Parameters:The function accepts three parameters as described below.

Return Type: This function returns an array of strings containing the separated segments.

Below program illustrates the working of explode() in PHP:




<?php
// PHP code to illustrate the working of explode()
  
$str1 = '1,2,3,4,5';
$arr = explode(',',$str1);
foreach($arr as $i)
echo($i.'<br>');
?>

Output:

1
2
3
4
5

You may refer to the article on PHP | explode() function to learn about explode() in details.

Important Points to Note:


Article Tags :