Difference Between implode and explode In PHP

In coding many times we need to convert a string into array and array into string for that we used explode and implode in php.
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. … The explode() function breaks a string into an array, but the implode function returns a string from the elements of 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. … The explode() function breaks a string into an array, but the implode function returns a string from the elements of an array

 

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 (“W”,”E”,”B”,”T”,”E”,”C”,”H”);

and you wish to combine it into a string, by putting the separator ‘-‘ between each element of the array.

How to do that?

$str = implode(“-“,$arr);

So your resulting string variable $str will be contain:

W-E-B-T-E-C-H

Syntax

string implode(string $seperator, array $array)

where,

$seperator  Optional, denotes the string pattern, such as empty string,that is placed between the cosecutive array elements to define  the resultant string

$array         Required, specifies the array that holds the elements to be   joined to form the resultant string

Example1

<html>

<body>

<h3>Webtechsource Implode Function</h3>

<?php

$arr=array (‘I’,’am’,’simple’,’boy!’);

echo implode(” “,$arr);

?>

</body>

</html>

OUTPUT : 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=”W E B T E C H”;

now you want to make each name as an element of an array and access it individually so what you do:

$arr = explode(“,”, $str);

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:

Array(

[0] => W

[1] => E

[2] => B

[3] => T

[4] => E

[5] => C

[6] => H

)

which is equal to: $arr = Array (“W”,”E”,”B”,”T”,”E”,”C”,”H”);

Syntax

int explode(string $seperator, string $string [, int $limit])

where,

$seperator   Required,used to seperate the words of a string

$string         Requires, specifies the string to be split

$limit           Optional, specifies the maximum number of string                      array elements that would be return to the function

Example1

<html>

<body>

<h3>Webtechsource Explode Function</h3>

<?php

$str=”I am simple boy!”;

print_r(explode(” “,$str));

?>

</body>

</html>

OUTPUT:

Array(

[0] => I

[1] => am

[2] => simple

[3] =>boy!

)

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

51 + = 61

%d bloggers like this: