PHP String explode() Function



The PHP String explode() function is used for splitting a string into individual strings. Basically this method separates a string according to a string delimiter and returns an array containing the strings produced by dividing the original string.

Syntax

Below is the syntax of the PHP String explode() function −

array explode ( string $delimiter , string $string [, int $limit ] )

Parameters

The explode() function needs three parameters and two of them are required and one optional. All three parameters are covered below −

  • $delimiter − This character identifies the critical points or points at which the string will divide; when this character appears in the string, it represents the end of one element of the array and the beginning of another.

  • $string − It is the input string that will be split into arrays.

  • $limit − This is optional. It shows the number of elements in the array. This argument can be any integer - positive, negative, or zero.

Return Value

The explode() function returns an array of strings produced by splitting the string given along the separator's limits.

If the separator is an empty string (""), explode() raises a ValueError. If the separator contains a value that is not in the string and the limit is negative, an empty array will be returned; otherwise, an array containing the string will be returned. If separator values appear at the beginning or end of a string, they will be appended as empty array values to the first or last positions of the returned array, correspondingly.

PHP Version

This function introduced in core PHP 4 and continues to work easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP String explode() function to split a string into an array of words.

<?php
// A sentence to split
$sentence = "Learn PHP easily with examples";

// Split sentence into words using space as a delimiter
$words = explode(" ", $sentence);

// Print the words
print_r($words);
?>

Output

Here is the outcome of the following code −

Array
(
   [0] => Learn
   [1] => PHP
   [2] => easily
   [3] => with
   [4] => examples
)

Example 2

In the below PHP code we will try to use the explode() function and extracts specified user data from a colon-separated text.

<?php
// User data in a colon-separated format
$userData = "amit:*:101:100:/home/amit:/bin/bash";

// Split the string into parts
list($username, $password, $uid, $gid, $homeDir, $shell) = explode(":", $userData);

// Print the extracted details
echo "Username: $username\n";
echo "Home Directory: $homeDir\n";
?> 

Output

This will generate the below output −

Username: amit
Home Directory: /home/amit

Example 3

This program explains how explode() function handles strings that do not have a delimiter. If the delimiter is not recognized in the string, explode() returns an array with the complete string as the only item. Otherwise, it will divide the string.

<?php
// Strings with and without the delimiter
$input1 = "welcome";
$input2 = "welcome,home";

// Use explode with a comma as the delimiter
print_r(explode(',', $input1));
print_r(explode(',', $input2));
?> 

Output

This will create the below output −

Array
(
   [0] => welcome
)
Array
(
   [0] => welcome
   [1] => home
)

Example 4

This program explains how to use the limit parameter in explode() function. The limit parameter defines how many splits are made. A positive integer limits the array's size but a negative value eliminates the last items from the result.

<?php
// String to split
$string = "apple|banana|cherry|date";

// Positive limit
print_r(explode('|', $string, 2));

// Negative limit
print_r(explode('|', $string, -1));
?> 

Output

Following is the output of the above code −

Array
(
   [0] => apple
   [1] => banana|cherry|date
)
Array
(
   [0] => apple
   [1] => banana
   [2] => cherry
)
php_function_reference.htm
Advertisements