The implode()
function converts an array into a string by inserting a separator
between each element.
This is the reverse of the explode()
function.
PHP implode() Function has the following syntax.
string implode ( string separator, array pieces )
Parameter | Meaning |
---|---|
separator | Separator to use to connect the array elements |
pieces | Array to implode |
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.
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.