PHP | Imploding and Exploding
Last Updated :
08 Mar, 2018
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
// 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
// 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.
Similar Reads
PHP implode() Function The implode() is a built-in function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function.If we have an array of elements, we can use the implode() function to join them all to form one string. We join
2 min read
PHP explode() Function The explode() function is an inbuilt function in PHP used to split a string into different strings. The explode() function splits a string based on a string delimiter, i.e. this function returns an array containing the strings formed by splitting the original string. Syntax:array explode(separator,
3 min read
PHP | exit( ) Function The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called. The message
2 min read
PHP File Handling In PHP, File handling is the process of interacting with files on the server, such as reading files, writing to a file, creating new files, or deleting existing ones. File handling is essential for applications that require the storage and retrieval of data, such as logging systems, user-generated c
4 min read
How to load classes in PHP ? PHP load classes are used for declaring its object etc. in object oriented applications. PHP parser loads it automatically, if it is registered with spl_autoload_register() function. PHP parser gets the least chance to load class/interface before emitting an error. Syntax: spl_autoload_register(func
2 min read
What is autoloading classes in PHP ? In order to use a class defined in another PHP script, we can incorporate it with include or require statements. However, PHP's autoloading feature does not need such explicit inclusion. Instead, when a class is used (for declaring its object etc.) PHP parser loads it automatically, if it is registe
2 min read
PHP | AppendIterator append() Function The AppendIterator::append() function is an inbuilt function in PHP which is used to append an iterator. Syntax: void AppendIterator::append( Iterator $iterator ) Parameters: This function accepts single parameter $iterator which holds the iterator to append. Return Value: This function does not ret
1 min read
PHP | include_once() and require_once() We already have learnt about file inclusion in PHP in the article PHP | (Include and Require). We have discussed about include() and require() functions for file inclusion in our previous article. In this article we will discuss about two more yet useful functions in PHP for file inclusion: include_
3 min read
PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi
8 min read
PHP | utf8_decode() Function The utf8_decode() function is an inbuilt function in PHP which is used to decode a UTF-8 string to the ISO-8859-1. This function decodes back to the encoded string which is encoded with the utf8_encode() function. Syntax:Â string utf8_decode( string $string ) Parameter: This function accepts single
2 min read