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

How to Trim Strings in PHP

The function of trimming strings is to remove the unwanted characters from string data.  String data requires trimming...
28/12/2020

How to Apply Try Catch Block in PHP

Exception handling is a very important feature of any object-oriented programming. When any logical or system error arrives...
29/12/2020

PHP and MySQL/MariaDB tutorial on Ubuntu

PHP and MySQL are the two most important tools to learn today for web programming. PHP is a server side programming language...
28/12/2020
Bài Viết

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

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024