PHP Tutorial - PHP implode() Function






Definition

The implode() function converts an array into a string by inserting a separator between each element.

This is the reverse of the explode() function.

Syntax

PHP implode() Function has the following syntax.

string implode ( string separator, array pieces )

Parameter

ParameterMeaning
separatorSeparator to use to connect the array elements
piecesArray to implode

Example 1

Convert array to string


<?PHP/*from   w  w w .  ja  va 2 s . c o m*/
$my = "A and B and C";
$my_array = explode(" and ", $my);
print_r($my_array);
print "\n";

$exclams = implode("! ", $my_array);
print_r($exclams);
print "\n";

$fruitArray  = array( "apple", "pear", "banana", "strawberry", "peach" ); 
$fruitString = implode( ",", $fruitArray ); 

echo $fruitString;    
         
?>

The code above generates the following result.





Example 2

The following code shows how to get a web page into a string.


<?php
$html = implode('', file('http://www.java2s.com/'));
echo $html;
?>

The code above generates the following result.