Explode And Implode In PHP

Explode And Implode In PHP

Here we show how you can transform a string in an array and an array change in a string. PHP provide to built in functions for these purpose  implode() and explode() .

Implode() – implode() built in function use to change an array into a string.

   Below is syntax 

				
					implode (separator, array)
				
			

We can transform a string from an array using implode() method. We just need to pass two parameter, One is array of strings and second is a delimiter of your response string.

The delimiter is used to joins them together into one string.

   Below is example

				
					<?php
  $arr = Array ("Hello,","I","am","php","developer.");
  $str = implode(" ",$arr); /* combine above array using separator ' ' */
  echo "<pre>";
  print_r($arr);
  echo $str;
 ?>
				
			

        OUTPUT

Explode()- explode() built in function use to change a string into an array.

     Below is syntax:

				
					explode (separator,string,limit)
				
			

Explode() function break a string into smaller text with each break occurring at the same symbol. This symbol is known as the separator(delimiter). Using the explode() method you can create an array from a string. The explode() function breaks a string into an array.

     Below is example

				
					<?php
 $str = "apple,banana,coconut,Grapes,mango";
 $arr = explode(",",$str); 
 
 echo $str;
 echo "<pre>";
 print_r($arr);
?>
				
			

        OUTPUT

You can split string into array using explode() method, and vise-versa easily using implode() method.

I hope this article helps you.
Thanks for visiting Inflay.com .