PHP Tutorials By Vineet Kumar Saini             Implode() and Explode() Function in PHPImplode() FunctionThe implode funct...
PHP Tutorials By Vineet Kumar SainiExample2<html><body bgcolor="pink"><h3>Implode Function</h3><?php$arr = array (I,am,sim...
PHP Tutorials By Vineet Kumar SainiExplode() FunctionThe explode function is used to "Split a string by a specified string...
PHP Tutorials By Vineet Kumar SainiSyntaxexplode (separator,string,limit)Example1<html><body bgcolor="pink"><h3>Explode Fu...
PHP Tutorials By Vineet Kumar SainiOutputConclusionSo in this article you saw how to use the implode and explode functions...
Upcoming SlideShare
Loading in …5
×

Implode & Explode in PHP

58,010 views

Published on

Published in: Education, Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
58,010
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
200
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Implode & Explode in PHP

  1. 1. PHP Tutorials By Vineet Kumar Saini Implode() and Explode() Function in PHPImplode() FunctionThe 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 ofstrings and joins them together into one string using a delimiter (string to be used betweenthe pieces) of your choice.The implode function in PHP is easily remembered as "array to string", which simplymeans that it takes an array and returns a string. It rejoins any array elements and returnsthe 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 elementof the array.How to do that?$str = implode("-",$arr);So your resulting string variable $str will be contain:A-E-I-O-USyntaximplode (separator , array)Example1<html><body bgcolor="pink"><h3>Implode Function</h3><?php$arr=array (I,am,simple,boy!);echo implode(" ",$arr);?></body></html>Output
  2. 2. PHP Tutorials By Vineet Kumar SainiExample2<html><body bgcolor="pink"><h3>Implode Function</h3><?php$arr = array (I,am,simple,boy!);$space_separated = implode(" ", $arr);$comma_separated = implode(" , ", $arr);$slash_separated = implode(" / ", $arr);$dot_separated = implode(" . ", $arr);$hyphen_separated = implode(" - ", $arr);echo $space_separated.<br>;echo $comma_separated.<br>;echo $slash_separated.<br>;echo $dot_separated.<br>;echo $hyphen_separated;?></body></html>Output
  3. 3. PHP Tutorials By Vineet Kumar SainiExplode() FunctionThe explode function is used to "Split a string by a specified string into pieces i.e. it breaksa string into an array".The explode function in PHP allows us to break a string into smaller text with each breakoccurring at the same symbol. This symbol is known as the delimiter. Using the explodecommand we will create an array from a string. The explode() function breaks a string intoan 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 sowhat you do:$arr = explode(",", $str);means : we have made pieces of string $text based on separator ,and put the resulting array in variable $arrSo I used print_r ($arr); and the results are the following:Array([0] => A[1] => E[2] => I[3] => O[4] => U)which is equal to:$arr = Array ("A","E","I","O","U");
  4. 4. PHP Tutorials By Vineet Kumar SainiSyntaxexplode (separator,string,limit)Example1<html><body bgcolor="pink"><h3>Explode Function</h3><?php$str="I am simple boy!";print_r(explode(" ",$str));?></body></html>OutputExample2<html><body bgcolor="pink"><h3>Explode Function</h3><?php$str="I am simple boy!";print_r(explode("-",$str));?></body></html>
  5. 5. PHP Tutorials By Vineet Kumar SainiOutputConclusionSo in this article you saw how to use the implode and explode functions in PHP. Using thisarticle one can easily understand the implode and explode functions in PHP.

×