The Wayback Machine - https://web.archive.org/web/20240618043737/https://www.geeksforgeeks.org/php-explode-function/
Open In App

PHP explode() Function

Last Updated : 20 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The explode() function is an inbuilt function in PHP used to split a string into different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This function returns an array containing the strings formed by splitting the original string.

Syntax:

array explode(separator, OriginalString, NoOfElements)

Parameters:

The explode function accepts three parameters of which two are compulsory and one is optional. All the three parameters are described below

  1. 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 end of one element of the array and the start of another.
  2. OriginalString: The input string that is to be split in an array.
  3. NoOfElements: This is optional. It is used to specify the number of elements of the array. This parameter can be any integer (positive, negative, or zero)
    • Positive (N): When this parameter is passed with a positive value it means that the array will contain this number of elements. If the number of elements after separating with respect to the separator emerges to be greater than this value the first N-1 elements remain the same and the last element is the whole remaining string.
    • Negative (N): If the negative value is passed as a parameter then the last N element of the array will be trimmed out and the remaining part of the array shall be returned as a single array.
    • Zero: If this parameter is Zero then the array returned will have only one element i.e. the whole string.
    • When this parameter is not provided the array returned contains the total number of elements formed after separating the string with the separator.

Return Type: The return type of the explode() function is an array of strings.

Examples:

Input : explode(" ", "Geeks for Geeks")
Output : Array(
[0] => Geeks
[1] => for
[2] => Geeks
)

The below program illustrates the working of explode() in PHP:

PHP




<?php
 
// original string
$OriginalString = "Hello, How can we help you?";
 
// Without optional parameter NoOfElements
print_r(explode(" ", $OriginalString));
 
// With positive NoOfElements
print_r(explode(" ", $OriginalString, 3));
 
// With negative NoOfElements
print_r(explode(" ", $OriginalString, -1));
 
?>


Output:

Array
(
[0] => Hello,
[1] => How
[2] => can
[3] => we
[4] => help
[5] => you?
)
Array
(
[0] => Hello,
[1] => How
[2] => can we help you?
)
Array
(
[0] => Hello,
[1] => How
[2] => can
[3] => we
[4] => help
)

Previous Article
Next Article

Similar Reads

Difference between split() and explode() functions for String manipulation in PHP
In this article, we will see the differences between split() and explode() functions for String manipulation in PHP. The split() and explode() functions are available in base PHP and are used to perform string manipulations as well as conversions. split() Function: The split() function in PHP is used to segregate the input string into different ele
3 min read
How to use Explode effect in jQuery ?
The Explode effect can be used with show/hide/toggle element. This explodes or implodes the element into/from many pieces. Syntax: selector.hide | show | toggle( "explode", {arguments}, speed ); Parameters: pieces: It holds the number of pieces. The default pieces are 9. Mode: The mode of the animation. The number of pieces to explode should be a p
1 min read
Underscore.js _.explode() Method
The _.explode() method is used to explode a string into an array of characters. Syntax: _.explode( String ); Parameters: String: This method takes a String to explode. Return Value: This method returns the generated character array. Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.
1 min read
Lodash _.explode() Method
The Lodash _.explode() method is used to explode a string into an array of characters. Syntax: _.explode( String ); Parameters: This method takes single parameter as mentioned above and described below: String: This method takes a String to explode. Return Value: This method returns the generated character array. Note: This will not work in normal
1 min read
jQuery UI Explode Effect
In this article, we are going to show the effect of explode using jQuery UI, all the content will break and spread out when the action button is clicked which actually triggers the explode effect script. Syntax: $( ".selector" ).effect( selectedEffect, options, time(in ms), callback ); Parameters: selectedEffect: Effect selected by developer given
2 min read
How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read
How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
PHP | Get PHP configuration information using phpinfo()
PHP provides us with a built-in function phpinfo() which gives us the details about the PHP version and PHP configuration of PHP installed in our system. To know about the Configurations and PHP version which is installed in your computer, a simple PHP script can be used. The script consists of a PHP function called "phpinfo()" which outputs inform
2 min read
PHP | php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It's very essential configuration file which controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this config
5 min read
Practice Tags :