PHP Implode and Explode Function

PHP provides us two functions Implode and Explode we use these functions to join the array elements and make it as string and to split the string based on the delimiter and create an array lets discuss implode and explode function in PHP one by one.

Implode: Join the array elements and make it as a string.

The implode() function returns a string from elements of an array. It takes an array of strings and joins them together into one string using a delimiter of your choice, delimiter is a separator of our choice it may be space, coma, semicolon, plus sign, hypen etc.

Explode: Split the string based on the delimiter and create an array.

The Explode function is used to break a string into array using a delimiter which must be in the string it may be space, coma, semicolon, plus sign, hypen etc.

It is generally used in the situations like insert values of check box selected by user here we don't know how much options a user select, if there are 5 check boxes  and we give option to the user to select according to there requirement, then user may select 3 or 4 options and now we have to insert the data selected by user then how we insert it into the table, so in this type of situation we use implode and explode function of PHP Lets see how we do this.

Lets take an example we have an array in which there are five colors.
$color="Red, Green, Blue, White, Black";

if we want to break this string and creates an array we can do this using explode funtion like below
Syntax:
explode('delimiter','string');

$arr=explode(',',$color);    //here we use coma as a delimiter
//Now we are going to print array values
echo $arr[0]."<br>";
echo $arr[1]."<br>";
echo $arr[2]."<br>";
echo $arr[3]."<br>";
echo $arr[4]."<br>";

If we want to join the array elements and make it as a string we can use implode function like below
Syntax:
implode('delimiter','Array');
$choice=implode(',',$arr);
echo $choice;

Now we are going to create a sample program to implement implode and explode funtion
Step1: Create a database test_db
           create database test_db;
           Create a table choice
           create table choice(color char(25));
Step2: Create index.php page

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
    <?php
          $con = mysql_connect('localhost','root','root','test_db');
          if (!$con) {
                 die('Could not connect: ' . mysql_error($con));
          }

        if(isset($_POST['submit']))
        {
             $choice=$_POST['col'];
            $choice1=implode(',',$choice);
            echo $choice1;
             mysql_query("insert into choice values('$choice1'");
        }
    ?>$color="Red, Green, Blue, White, Black";
   <form method="post" action="">
                Select your favourite color:
                Red:<input type="checkbox" name="col[]" value="red">
               Green:<input type="checkbox" name="col[]" value="green">
               Blue:<input type="checkbox" name="col[]" value="blue">
               White:<input type="checkbox" name="col[]" value="white">
               Black:<input type="checkbox" name="col[]" value="black">
              <input type="submit" name="submit" value="Submit">
  </form>
  </body>
  </html>

In this example user select the colors and we have to insert the choice into single column using the implode function and we use the delimiter coma, so if we retrieve the data from the database it is in the string and we have to use the explode function with the delimiter coma. 


Output:

PHP Implode and Explode Function



Conclusion:
We can store multiple choice in a single column we don't require a separate column to store check box values.

Post a Comment

1 Comments

  1. Nice Tutorial, many beginners programmers not know the implode and explode function and their use, its a nice example for them.

    ReplyDelete