PHP Explode Tutorial

28/12/2020
php
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[$length1] . ‘<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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

PHP Explode Tutorial

PHP is one of the powerful website development tools that contains various built-in functions for various purposes. explode()...
28/12/2020

PHP Server Side Charts Tutorial

A very common requirement of business websites is creating charts based on the collected data of the website to find out...
28/12/2020

How to Install Jetbrains PHPStorm on Ubuntu

PHPStorm by JetBrains is one of the best PHP IDE. It has plenty of amazing features. It also has a good looking and user...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024