Implode() and Explode() Function in PHP

Implode() Function
The implode function is used to “join elements of an array with a string”.
The implode() function returns a string from elements of an array. It takes an array of strings and joins them together into one string using a delimiter (string to be used between the pieces) of your choice.
The implode function in PHP is easily remembered as “array to string”, which simply means that it takes an array and returns a string. It rejoins any array elements and returns the resulting string, which may be put in a variable.
Suppose you have an array like this $arr = Array (“A”,”E”,”I”,”O”,”U”);
and you wish to combine it into a string, by putting the separator ‘-‘ between each element of the array.
How to do that?

So your resulting string variable $str will be contain: A-E-I-O-U
Syntax

Example1 –

Output- I am simple boy!
Example 2

Output—–
I am simple boy!
I, am, simple, boy!
I /am /simple/ boy!
I .am. simple. boy!
I -am- simple- boy!
Explode() Function
The explode function is used to “Split a string by a specified string into pieces i.e. it breaks a string into an array”. The explode function in PHP allows us to break a string into smaller text with each break occurring at the same symbol. This symbol is known as the delimiter. Using the explode command we will create an array from a string. The explode() function breaks a string into an array, but the implode function returns a string from the elements of an array.
For example you have a string
$str=”A E I O U”;
now you want to make each name as an element of an array and access it individually so what you do:

means : we have made pieces of string $text based on separator ‘,’ and put the resulting array in variable $arr So I used print_r ($arr); and the results are the following:

which is equal to: $arr = Array (“A”,”E”,”I”,”O”,”U”);
Syntax

Example 1

Output- Array ( [0] => I [1] => am [2] => simple [3] => boy! )
Example 2

Output – Array ( [0] => I am simple boy! )
Conclusion
So in this article you saw how to use the implode and explode functions in PHP. Using this article one can easily understand the implode and explode functions in PHP.

Anuj Kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Recommended Tutorials for you

You may also like...

Leave a Reply

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