What is explode() function in PHP?


In this article, figure out how to utilize PHP Explode() function which is a predefined inbuilt PHP function.

The explode function is utilized to "Split a string into pieces of elements to form an array".The explode function in PHP enables us to break a string into smaller content with a break. This break is known as the delimiter.

Syntax

explode(separator, String, Number of Elements)

Parameters

The explode function acknowledges three parameters of which two are compulsory and one is optional.

Let's discuss the three parameters.

separator

This character specifies the critical points or points at which the string will split, i.e. whenever this character is found in the string it symbolizes the end of one element of the array and start of another.

String

The input string is to be split in the array.

Number of Elements

This is an optional parameter. It is utilized to determine the number of elements of the array. This parameter can be any integer(positive, negative or zero)

Note

When this parameter is not provided the array returned contains the total number of the element formed after separating the string with the separator.

Example

<?php
   $Original = "Hello,Welcome To Tutorials Point";
   print_r(explode(" ",$Original));
   print_r(explode(" ",$Original,3));
?>

Output:

Array ( [0] => Hello,Welcome [1] => To [2] => Tutorials [3] => Point )
Array ( [0] => Hello,Welcome [1] => To [2] => Tutorials Point )

Explanation

In the above example, in the first expression, we have not passed the third parameter and we are only creating the new array with the help of "space" delimiter but in the second expression, we have instructed to create the new array with only three elements by passing the third parameter.

Updated on: 29-Jun-2020

514 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements