Posted By:
8 Mar, 2014 2:14 am PST

How to use explode() and implode() function of String in php ?

 How to use explode() and implode() function of String in php ?

String is nothing but sequence of character in double quotes (“ “).String can contains combination of alphabetic , numbers ,special symbol .

Ex:-

$email= abx.xyz19@gmail.co.in

 

explode() :-

This function takes input as string and separator character (including space) and returns array of string as output. This function breaks the string into array of substring based on separator value.

Syntax:-

explode(separator , string , limit) ;

 

limit is Optional. Specifies the number of array elements to return

implode():-

 

This function takes input as array of string and separator value and returns single string combining elements of array. It combines each element substring into single String. 

Syntax:-

Implode(separator , array);

Example

<?php

 

echo " <br > < h3 > " ;

$explodeString = " Mankind must put an end to war before war puts an end

to mankind . <br > " ;// orignal string

$explodeArray = explode ( " " , $explodeString ) ; // explode

 

echo " Original String is \"{ $explodeString }\" <br > Content of Array

After explode function <br > " ;

echo " <pre > " ;

print_r ( $explodeArray ) ;

// print_r () information about a variable in a way displays that sreadable by humans

 

echo " <br > < br > < br > " ;

$impodeArray = array ( " The " ," best " ," weapon " ," against " ," an " ," enemy " ,"←

is " ," another " ," enemy . " ) ;

 

$implodeString = implode ( "" , $impodeArray ) ; // implode

 

 

echo " Original Array is \" " ;

print_r ( $implodeString ) ;

echo " \" " ;

echo " <br > String After implode function \" <br >$explodeString } \" .";

 

?> ExplodeImplode.php

 

//to demonstrate explode() and implode() function

 

InplodeExplodeOutput 

Attachment: ExplodeImplode.php


How to use implode() and explode() in PHP?

 

»implode() :This is used to implode array to String that is it convert array elements to a String.This function work to join the array to String.

SYNTAX:

implode(separator,array);

 

»explode():

This function work opposite of implode function.It is used to convert String to array format.

SYNTAX:

explode(separator,string,limit)

 

//Program that illustrate how to use implode() and explode() in PHP.

<html>

<body>

<?php

$subject[0] = "PHP";

$subject[1] = "is";

$subject[2] = "Server";

$subject[3] = "Scripting";

$subject[4] = "Language";

$content = implode(" ", $subject);  //implode function is used 

echo $content,"<br>"."<br>";

$str = "Hello everyone. This is PHP Programming.";

print_r (explode(" ",$str));      //explode function is used

?>

</body>

</html>

 

/*Output of th:e program is:

PHP is Server Scripting Language

Array ( [0] => Hello [1] => everyone. [2] => This [3] => is [4] => PHP [5] => Programming. ) ?>

*/

 

 

 

 

 

Was it useful?  




Please login to reply to this problem.