PHP | explode() Function

03 Jan, 2022
explode() is a built in function in PHP used to split a string in 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 functions 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
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 start of another.OriginalString : The input string which is to be split in array.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 negative value is passed as 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 element formed after separating the string with the separator.
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 start of another.
OriginalString : The input string which is to be split in array.
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 negative value is passed as 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 element formed after separating the string with the separator.
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 negative value is passed as 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 element formed after separating the string with the separator.
Return Type: The return type of explode() function is array of strings.
Examples:
Input : explode(" ","Geeks for Geeks")
Output : Array
        (
            [0] => Geeks
            [1] => for
            [2] => Geeks
        )

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
)

Reference: http://php.net/manual/en/function.explode.php
JaiwardhanSwarnakar
gabaa406
PHP-string
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
REST API (Introduction)
Node.js fs.readFileSync() Method
How to set the default value for an HTML <select> element ?
Roadmap to Learn JavaScript For Beginners
How to create footer to stay at the bottom of a Web page?
How to set input type date in dd-mm-yyyy format using HTML ?