Menu

explode and implode function in PHP

  • explode function in PHP split the string into an array by using the separator.
  • The separator cannot be blank.

Syntax:

               explode(separator,string,limit)

  • separator => Required, It indicates where to break the string
  • string => Required, The string in which the operation to be carried out.
  • limit => Limit, Specifies the count of the number of elements returns.  

Example:

   <?php
        $string = "This is a string for testing explode function.";
        print_r (explode(" ",$str));
    ?> 

Output:

Array ( [0] => This [1] => is [2] => a [3] => string [4] => for [5] => testing. )

  • Implode function return a string from an array. It seperates the array elements with different characters.
<?php
 $array_test = array('This','is','for','testing');
 echo implode(" ",$array_test)."<br>"; // This is for testing
 echo implode("+",$array_test)."<br>"; // This+is+for+testing
 echo implode("-",$array_test)."<br>"; // This-is-for-testing
?>