Explode And Implode Function In Php

In this tutorial, we are going to learn a very handy function which you need, when you working on your project. In many situations, you want to break a PHP string at a particular place and gather a broken string on particular array position or either you want to convert an array to a string. For this, we use Explode And Implode Function In Php.

What is Explode and Implode Function in PHP?

So, There are two functions in PHP which we can use to perform such task and we are going to learn it one by one.

1.Explode function

2.Implode function

One by one we are going to study this two function that how they perform.

Explode and Implode function in php

Explode Function:

Php has an explode function which is used to break a string and convert it to an array. In Simple term, you can refer it as a “string to array” because when you explode a string using a particular string it breaks from that particular string and their segment is saved into an array.

Basic syntax of explode function is explode(separator,string,limit)

separator-Here you need to define a word or string from where you want to break a string.

string-Here we place our string which we want to break.

limit-It is used for limiting an array.

Let’s take an example.

By default, if we do not define a limit then it will be -1 which means max array. As you can see on above example that we are using comma as a separator or as a splitter from where a string is split into an array.


explode and implode function

In above result, at zero indexes complete string is placed because we define limit 0But when we define limit 2 on second explode function it prints an array with 2 indexes. But when we do not define any limit in last explode function it shows completely broken a string in an array.

Implode Function:

Php has an implode function which is used to join array and convert it to one string. In Simple term, you can refer it as an “array to string” because when you implode an array using a particular string it join from that particular string and their segment is joined into a string.

basic syntax of explode function is implode(separator,array)

separator-Here you need to define a word or string which you want to be put between array.

array-Here we place our array which we want to join.

Let’s take an example.


explode and implode funtion in php

If we want to replace a word from a string then we can combine the explode and implode function to accomplish that task.

Let’s see in example;


As we see that how we break the PHP string and convert into an array and implode function which allows as to again join an array to a string.We also replace a word from the string as we can see in above example.

I hope if you like this post then do not forget to comment below if you have any problem regarding this tutorial. And don’t forget to share it on Facebook and other social media.


Also Read:

  1.  PHP Tutorial – PHP Introduction
  2.  Php Form Submit Using Post And Get Method

Leave a Reply

Your email address will not be published. Required fields are marked *