PHP implode() and explode() are the couple of popularly used string functions. Both are used to convert string or array data from one another and these are complement to each other.
implode() function accepts a separator and an array as its parameter. It combine each of the array elements whereas the combined elements are separated with the separator. For example,
$str_concat = "|"; $meaning_array = array("implode","join","concatenate"); $imploded_string = implode($str_concat,$meaning_array); echo $imploded_string; // implode|join|concatenate
This program will return combined array elements separated by the (|) symbol. There is no strict rules for the arguments order of this function. For example, we can use implode as like as,
... $imploded_string = implode($meaning_array,$str_concat); ...
On the other hand, explode() is to split string into an array of elements with respect to the given separator. explode() accepts separator and a string as its argument.
$input_string = "implode|join|concatenate"; $str_explode = "|"; $exploded_array = explode($str_explode,$input_string ); echo "<pre>"; print_r($exploded_array); echo "</pre>";
This program accepts the resultant string data of implode example we have seen above. As we given the same separator string (|), explode() splits string data and reproduce the original array we have given for implode. The output of this program will be
Array (  => implode  => join  => concatenate )
explode() is used as an alternative for split(), a deprecated PHP function. We have seen the reason for why split() is deprecated.
PHP explode() accepts a third optional argument limit to set target length or boundary for performing explode operation. explode() will take entire string if nothing is specified as limit.
$exploded_array = explode($str_explode,$input_string,2);
this will return,
Array (  => implode  => join|concatenate )
This PHP tutorial was added on May 30, 2013.