PHP implode and explode function with example

PHP implode and explode function with example

In this article lets understand how to use PHP implode and explode function with example here.

Implode and explode functions are used for the purpose like creating array from string and/or creating string from array. Let us understand it step by step with it’s definitions, parameters and examples.

1.  explode() function

Syntax :

explode($separator, $string, $limit)
  • $separator – string used to separate, delimiter we can say.
  • $string – is the input string to be split
  • $limit – optional (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.

$url = "www.codingsolutionspoint.com";
$exploded_url = explode('.',$url);
print_r($exploded_url);

Result :

Array ( [0] => www [1] => codingsolutionspoint [2] => com )

2.  implode() function

Syntax :

implode (delimiter,array)
  • delimiter – optional (will merge array elements by delimiter string)
  • array – collection of elements that merged into a string

Return Type: This function returns string.

 $urlArray =  [ 'www','codingsolutionspoint','com' ];
 $url = implode('.',$urlArray);
 echo $url;

implode will do the reverse of explode. It will return a string merged by given array and delimiter.

Result :

www.codingsolutionspoint.com

We use these functions in PHP for tasks like parsing CSV files, generating comma-separated lists, or manipulating strings in various ways. String manipulation is one of the popular one.

That’s all on PHP implode and explode function with example. Hope this finds you helpful.

See other articles on PHP here.