PHP String Explode and Implode

In this tutorial we are going to learn about the PHP String Explode and Implode which use to explode and implode the any string in PHP.

PHP String Explode and Implode


Imploding and Exploding are couple of important functions of PHP that can be applied on strings or arrays. PHP provides us with two important builtin functions implode() and explode() to perform these operations. As the name suggests Implode/implode() method joins array elements with a string segment that works as a glue and similarly Explode/explode() method does the exact opposite i.e. given a string and a delimiter it creates an array of strings separating with the help of the delimiter.

PHP Explode


The explode function is awesome because it breaks a string into manageable parts, like words. You can easily create a PHP program using explode and other PHP functions to create a program that performs a word count. I crafted a program like this in college because I have a terrible tendency to use the same words, and apparently professors think that means you have a limited mind. I showed them. Enough bragging, more coding.

Syntax

implode (separator , array)

Example

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

Output

Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )

We employ our print_r class to print out our array in a way that is readable. Inside that function, we have our explode function. First, we say that we want to separate everything by spaces, ” “, and we want to break up $myString. We could pass anything we want into the first parameter, and PHP will find any matches and explode the parts around it into an array. However, when you match something using explode, it does not include the matched string. In our example, we matched spaces, but in our array the spaces are completely eliminated. There is a third optional parameter to limit the number of words you want to break up, which you can add on the end. PHP Explode is finished. Let’s be more constructive in the next section.

PHP Implode


Enough of blowing things apart. Let’s start putting them back together with the PHP implode function. The implode function essentially does the opposite of the explode function. You can take an array and join it together and make it into one string instead of an array. Ok, we’re sorry. We’ll put it back together.

Syntax :

string implode(separator,array)

Parameters: The implode() function accepts two parameter out of which one is optional and one is mandatory.

  1. separator: This is an optional parameter and is of string type. The values of the array will be join to form a string and will be separated by the separator parameter provided here. This is optional, if not provided the default is “” (i.e. an empty string).
  2. array: The array whose value is to be joined to form a string.

Note: The separator parameter of implode() is optional. However, it is recommended to always use two parameters for backwards compatibility.

Return Type: The return type of implode() function is string. It will return the joined string formed from the elements of array.

Example:

<?php
    // PHP Code to implement join function
     
    $InputArray = array('Programming','for','You');
     
    // Join without separator
    print_r(implode($InputArray));
     
    print_r("\n");
     
    // Join with separator
    print_r(implode("-",$InputArray));
?>

Output:

ProgrammingforYou
Programming-for-You

Our code is almost identical to the example above of the PHP String Explode and Implode, but we are not printing it out until we use our next function implode. In our implode function, we simply say we want to join all of the items in our array $newArray together, but please separate them with a space, ” “. Boom! We blew a string into pieces, and then put it again together like a boss.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.