php Web Programming

PHP Explode Tutorial

PHP is one of the powerful website development tools that contains various built-in functions for various purposes. explode() method is one of the built-in PHP functions which is used to split string data into multiple parts based on any particular delimiter.  This function returns an array after splitting the string or text data. Every part of the string can be easily retrieved by accessing the array. Different use of explode() function are shown in this tutorial.

Syntax:

explode(‘delimiter’,’string’,[limit])

Here, the first two parameters are mandatory parameters and the last parameter is optional.  The searching string will be defined in the first parameter and the main string which will be split will be defined in the second parameter. Any numeric value can be set as optional parameter which will set the limit of splitting.

Example-1: Using empty string delimiter

In the following example, a single space is used as delimiter which will split $mystr string into three words. Here, $parts is the array of three elements which contains the return values of explode() function. Save the code in a php file and store it on web server location.

<?php
$mystr='I like Programming';
//space is used as delimiter
$parts=explode(' ', $mystr);
echo $parts[0] . '<br/>';
echo $parts[1] . '<br/>';
echo $parts[2];
?>

Output:

When you will run the code from localhost then the following output will appear.

Example-2: Using particular character as delimiter

Without space, any character or string can be used as delimiter. In the following code, ‘easy’ is used as delimiter. After using explode, the string will be divided into two parts and $parts array will contain two elements which are printed later.

<?php
$mystr='Learn programming with easy tutorials of linuxhint.com';
//’easy’ is used here as delimiter
$parts=explode('easy', $mystr);
//print the structure of the created array
print_r($parts);
echo '<br/>'. $parts[0] . '<br/>';
echo $parts[1];
?>

Output:

When you will run the code from localhost then the following output will appear.

Example-3: using list function to read the value of returning array

list is another built-in function of PHP which reads values of any array into variables. In the following example, ‘;’ is used as delimiter and the return values of explode function are retrieved by three variables named $a, $b and $c using list function.

<?php
$mystr = 'hostname:localhost;username:root;password:abc123';
list($a,$b,$c)=explode(';',$mystr);
echo $a . '<br/>';
echo $b . '<br/>';
echo $c;
?>

Output:

When you will run the code from localhost then the following output will appear.

Example-4: Using positive limit parameter

explode() function accepts positive or negative number as third parameter.  Using positive limit value in explode() function, the number of splitting can be reduced. If you explode the following string without limit then it will create an array of 6 elements. 5 is used as limit value in the following example. For this, the last elements of the array will be created by combining last two months.

<?php
$mystr = 'January>February>March>April>May>June';
//Set positive value as limit
$output=explode('>',$mystr,5);
print_r($output);
echo '<br/>Last element of the array is: ' . $output[4] . '<br/>';
?>

Output:

When you will run the code from localhost then the following output will appear.

Example-5: Using negative limit parameter

In the following example, negative values is used as limit. When you assign any negative value in a function then it counts from the last part of the string or array. For the value -2, the last 2 weekday name will not be added as array elements.

<?php
$mystr = 'Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday|Friday';
//set negative value as limit
$output=explode('|',$mystr,-2);
//Count total number of array elements
$length=count($output);
print_r($output);
echo '<br/>Total elements of the array is: ' . $length;
//Print the last element of the array
echo '<br/>Last element of the array is: ' . $output[$length-1] . '<br/>';
?>

Output:

When you will run the code from localhost then the following output will appear.

The concept of explode function will be cleared after practicing the above examples properly. There is another built-in function of PHP named implode which is the opposite of the explode function.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.