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.
- glue (optional): This parameter expects a string segment that is used as the glue to join the pieces of the array. This is an optional parameter with default value being an empty string.
- pieces: This parameter expects an array whose elements are to be joined together to construct the final imploded string.
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.
- delimiter: This parameter expects a string segment that can be used as the separator. If the delimiter itself is not present in the string then the resultant will be an array containing the string as its sole element.
- string: This parameter expects a string which is to be exploded.
- limit: This parameter expects an integer (both positive and negative). This is an optional parameter with default value of PHP_INT_MAX. The limit refers to the maximum number of divisions that are to be made on the input string.
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:
- The implode() function implodes the elements of an array and returns the resultant string.
- Although not advised, the implode() function can take the parameters irrespective of their order
- The PHP | Join() function is an alias of implode() function.
Recommended Posts:
- How to access an object having spaces in the object's key using JavaScript ?
- How to move an element to left, right, up and down using arrow keys ?
- How to check a checkbox with jQuery?
- What is the Efficient way to insert a number into a sorted array of numbers?
- How to merge the duplicate value in multidimensional array in PHP ?
- How to Show and Hide div elements using Checkboxes ?
- How to get the first key name of a JavaScript object ?
- How to animate div width and height on mouse hover using jQuery ?
- How to make horizontal scrollable in a bootstrap row?
- What is the difference between offsetHeight and clientHeight ?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- How to add sleep/wait function before continuing in JavaScript?
- PHP Full Form
- Laravel | Installation and Configuration
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



