PHP implode and explode Functions | PHP Tutorial For Beginners

PHP explode() Function

explode() breaks a string into an array, using a separator you specify.

Syntax: explode(separator, string)

  • separator — the character (or string) that marks where to split.
  • string — the text to split.

Example

<?php
$string = "PHP tutorial to use explode and implode functions for beginners.";
$data   = explode(" ", $string);

foreach ($data as $part) {
    echo $part . "<br>";
}
?>

Output:

PHP
tutorial
to
use
explode
and
implode
functions
for
beginners.

PHP implode() Function

implode() joins array elements into a string, placing a separator between each element.

Syntax: implode(separator, array)

Example — join with a space

<?php
$words = array("PHP","tutorial","to","use","explode","and",
               "implode","functions","for","beginners.");
$data  = implode(" ", $words);
echo $data;
// Output: PHP tutorial to use explode and implode functions for beginners.
?>

Example — join with a hyphen

<?php
$words = array("PHP","tutorial","to","use","explode","and",
               "implode","functions","for","beginners.");
$data  = implode("-", $words);
echo $data;
// Output: PHP-tutorial-to-use-explode-and-implode-functions-for-beginners.
?>

Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.

← Older Post Newer Post →