PHP explode

PHP explode is a function that is used to convert a string into an array. If you have a string of 10 words, then the PHP explode function will convert it into the form of an array. Let’s know how to use explode in PHP.

PHP Explode Function

<?php
    $string = "how to use explode in php";
    echo "<pre>";
    print_r (explode(" ",$string));
    echo "</pre>";
?>

Output:- 

Array
(
    [0] => how
    [1] => to
    [2] => use
    [3] => explode
    [4] => in
    [5] => php
)

/ ***** Example 2 ***** /

<?php
    $string = "how to use explode in php";
    echo "<pre>";
    print_r (explode(" ",$string, 2));
    echo "</pre>";
?>

Output:- 

Array
(
    [0] => how
    [1] => to use explode in php
)

/ ***** Example 3 ***** /

<?php
    $string = "how to use explode in php";
    echo "<pre>";
    print_r (explode(" ",$string, 4));
    echo "</pre>";
?>

Output:- 

Array
(
    [0] => how
    [1] => to
    [2] => use
    [3] => explode in php
)

Other PHP Explode Example

<?php
    
    $number = "100-200-300"; 
    
    $result = explode("-", $number);
    echo "I have = $result[0]<br />";
    echo "You have = $result[1]<br />";
    echo "They have = $result[2]";
    
?>

Output:- 

I have = 100
You have = 200
They have = 300

/ ***** Using For Loop ***** /

<?php
    
    $longstring = "This is a long string"; 
    
    $result = explode(" ", $longstring);
    for($i = 0; $i < count($result); $i++){
    	echo "No - $i = $result[$i] <br />";
    }
    
?>

Output:- 

No - 0 = This
No - 1 = is
No - 2 = a
No - 3 = long
No - 4 = string

PHP Implode Function

If you have an array that you want to convert to string, then you have to use PHP Implode Function.

<?php
    
    $string = array('This','is','dummy','content');
    
    $result = implode(" ", $string);
    echo $result;
    
?>

Output:- This is dummy content

/ ***** Other Example ***** /

<?php
    
    $string = array('This','is','dummy','content');
    
    $result = implode(", ", $string);
    echo $result;
    
?>

Output:- This, is, dummy, content

Browser Support

  • Edge
  • Firefox
  • Internet Explorer
  • Opera
  • Safari
  • Chrome

Frequently Asked Questions

We have answers to some questions that people search on the Internet every day. Let’s know what those questions are and what their answers are.

How do I link CSS to HTML?
To link CSS with html, you can place your file inside the head tag. For example link rel = “stylesheet” href = “mystyle.css”

How do you put a full background image on HTML?
If you want to apply the full background image, then you can apply the image above your body tag. for example Background-image: url (“myimage.jpg”).

What is div in HTML?
In simple language, div is a box inside which you set your elements.

How do you underline in HTML?
With the u tag you can underline your text.

Where do I put CSS in HTML?
There are 3 ways to apply CSS, either you can link your file inside the head tag, or you can add inline CSS, or you can place your CSS on the same page with the style tag.

What is text formatting in HTML?
To make the text appear well, the text is formatted with a few HTML tags.

Note: All the examples you have seen inside this article have all been made by us and tested thoroughly. So you can use all these without any difficulty.

Related: 10 awesome examples of javascript else if condition