Explode PHP Function (With Detailed Explanation)

The Explode PHP is a built-in function which mainly used to convert a string into array.

In this article, we will talk about PHP explode() in a detailed explanation as well as example programs that could be helpful to your learning on this function. This topic is a continuation of the previous topic, entitled PHP Substring.

What is explode in PHP?

In PHP, explode() is a function that used to turn a string into an array. Further, every character in a string has specified an index which starts from 0 and the function doesn’t change the given string it only converts it into an array.

Syntax

explode(separator,string,limit)

The PHP explode() function have 3 parameters.

  • separator
  • string
  • limit

Parameter values

PARAMETER DESCRIPTION
separatorThis parameter is (REQUIRED) and defines a string where to break.
stringThis parameter is (REQUIRED), and this string parameter is used to split.
limitThis parameter is (OPTIONAL), and this parameter is used to define the number of array elements that will return.

Possible values…

Once the parameter is Greater than 0. It will return an array with a maximum number of limit parameter elements.
Once the parameter is Less than 0. It will return an array excluding the last limit elements.
Once the parameter is 0. It will return an array with just a single element.

Example

<?php
         print_r (explode(" ","I'm Glenn Magada Azuelo, a Student, Writer, and a Programmer."));
?>

Output

Array
(
    [0] => I'm
    [1] => Glenn
    [2] => Magada
    [3] => Azuelo,
    [4] => a
    [5] => Student,
    [6] => Writer,
    [7] => and
    [8] => a
    [9] => Programmer.
)

Why explode () is used?

The explode() is a function which used to break a string into pieces of elements to become an array of strings.

Further, the PHP explode function can enable to break a string into smaller words with the use of (break). The break is also referred as (delimiter).

Example

<?php

	// original string without throws a valueerror and empty string
	$name = "I'm Glenn Magada Azuelo, a Student, Writer, and a Programmer.";
	
	// Without optional parameter NoOfElements
	print_r(explode(" ",$name));
	// with positive NoOfElements
	print_r(explode(" ",$name,3));
	// with negative limit NoOfElements
	print_r(explode(" ",$name,-1));
	
?>

Output

Array
(
    [0] => I'm
    [1] => Glenn
    [2] => Magada
    [3] => Azuelo,
    [4] => a
    [5] => Student,
    [6] => Writer,
    [7] => and
    [8] => a
    [9] => Programmer.
)
Array
(
    [0] => I'm
    [1] => Glenn
    [2] => Magada Azuelo, a Student, Writer, and a Programmer.
)
Array
(
    [0] => I'm
    [1] => Glenn
    [2] => Magada
    [3] => Azuelo,
    [4] => a
    [5] => Student,
    [6] => Writer,
    [7] => and
    [8] => a
)

What is the purpose of the explode() and implode() functions in PHP?

The explode() and implode() function has their own unique purposes in PHP, the explode() function purpose is to convert an input string separator into array. While the implode() function purpose is to return a given string from the number of elements of an array.

What is difference between split and explode in PHP?

The function split and explode in PHP were both used to split a string into an array. Yet, the difference is the explode() function is much faster than split() for the reason that it does not match on the regular expression. While the split() function only uses a pattern for splitting a remaining string.

Summary

In summary, you have learned about Explode PHP. This article also discussed what explode is, why explode () is used, the purpose of the explode() and implode() functions, and the difference between split and explode.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment