PHP explode() Definition and Example

PHP explode() is a function that split a string by the delimiter and returns into an array of string.

The definition of PHP explode()

explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) : array

explode() has three parameters, and the third parameter is optional.

$delimiterThe boundary string.
$stringThe input string.
$limitIf $limit is set and positive, the returned array will contain a maximum of $limit elements with the last element containing the rest of $string.
If the $limit parameter is negative, all components except the last –$limit are returned.
If the $limit parameter is zero, then this is treated as 1.

Example of PHP explode()

The explode function very useful. We will have an example of tags for blog post to explain how to use PHP explode() function.

In the HTML part, you can use sliptree/bootstrap-tokenfield for tags input. After the user submits the form of the blog post, the server-side will receive the tags in $_POST array such as "PHP, explode, how to use, string to array", etc.

Before we insert these tags into the database, we can simply insert the tags string “PHP, explode, how to use, string to array” just into the database field tags. And, we can split the tags into an array and insert each tag into a single row. Also, we can have a post-tag relation table, to analyze posts and tags.

Now we have the HTML page:

<html lang="en">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- css -->
    <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
    type="text/css" />
    <link href="[email protected]/dist/css/bootstrap-tokenfield.min.css" rel="stylesheet"
    type="text/css" />
    <!-- javascript -->
    <script src="[email protected]/dist/jquery.min.js"></script>
    <script src="[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <script src="[email protected]/dist/bootstrap-tokenfield.min.js"></script>
    <script type="text/javascript">

    <div class="container">
        <form method="post" action="index.php">
                PHP explode() example
            <div class="form-group">
                <label for="blog-title">Blog Title</label>
                <input type="text" class="form-control" id="blog-title" name="title">
            <div class="form-group">
                <label for="blog-content">Blog Content</label>
                <textarea class="form-control" id="blog-content" rows="10" name="content"></textarea>
                <!-- You can use wysiwyg editor such as tinymce instead of teatarea-->
            <div class="form-group">
                <label for="blog-tags">Tags</label>
                <input type="text" class="form-control tokenfield" id="blog-tags" name="tags">
            <button type="submit" class="btn btn-primary">Submit</button>


And the server-side index.php:



if(!empty($_POST)){// if this is a post method and the post data is not empty
    $title = $_POST['title']; // sterilize in production mode
    $content = $_POST['content']; // sterilize in production mode
    $tags = $_POST['tags']; // sterilize in production mode

        $tag_array = explode(',',$tags);

        // now we got the tag array
        // transaction start
        // insert the blog post to table_blog_post and get the blog post id
        // insert batch all the tags to table_tag
        // add the relationship with blog_post_id and tag_id to table_blog_relate_tag
        // transaction commit or transaction rollback


You can create the example file to test the PHP explode() function.

And you can download or clone the example code from bruce-lab/php-explode.

Leave a Comment

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