What is difference between explode and split in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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 elements. The elements are divided based on the occurrence of patterns in the string. The optional parameter depicts the number of elements to divide the array. It begins from the left to the right of the string. The method returns an array. 

    array split (string pattern, string string [, int limit])

    Parameters: 

    • pattern – The separator to break the string.
    • string – The string to split into parts.

    PHP

    $str = "Geeks_for_geeks_is_fun!";

    $arr = split("\_", $str);

    print("String components : ");

    print_r($arr);

    ?>

    Output

    String components : Array ( 
        [0] => Geeks 
        [1] => for 
        [2] => geeks 
        [3] => is 
        [4] => fun! 
    )

    explode() Function: The explode() function in PHP is used to divide the string into array components depending on the separator specified. The limit parameter indicates the number of parts into which to divide the array into. This method returns an indexed array with indexes mapped to array elements. 

    explode(separator, string, limit)

    Parameters: 

    • separator – The separator break the string.
    • string – The string to split into parts.
    • limit – Indicator of the number of array elements to divide the array into parts. 
      • More than 0 – Returns an array with a maximum of limit element(s)
      • Less than 0 – Returns an array except for the last –limit elements()
      • Equal to 0 – Returns an array with one element

    PHP

    $str = "Geeks for geeks is fun!";

    $arr = explode(" ", $str);

    print("String components : ");

    print_r($arr);

    ?>

    Output

    String components : Array
    (
        [0] => Geeks
        [1] => for
        [2] => geeks
        [3] => is
        [4] => fun!
    )
    

    The following code snippet divides the string into a single array component: 

    PHP

    $str = "Geeks for geeks is fun!";

    $arr = explode(" ", $str, 0);

    print("String components : ");

    print_r($arr);

    ?>

    Output

    String components : Array
    (
        [0] => Geeks for geeks is fun!
    )
    

    The following code snippet indicates the usage of the last parameter less than 1: 

    PHP

    $str = "Geeks for geeks is fun!";

    $arr = explode(" ", $str, -1);

    print("String components : ");

    print_r($arr);

    ?>

    Output

    String components : Array
    (
        [0] => Geeks
        [1] => for
        [2] => geeks
        [3] => is
    )
    

    split() Function

    explode() Function

    Uses a pattern to divide the string. Uses a string separator to divide the string.
    More execution time. Leas execution time.
    Matches the string based on a regular expression. Doesn’t match string based on the regular expression.
    Deprecated as of PHP 5.3 Still in use.

    What is the difference between explode and split functions in PHP?

    split() Function: The split() function in PHP is used to segregate the input string into different elements. ... PHP..

    What does explode () do in PHP?

    The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string.

    Why explode () is used?

    The explode function is utilized to "Split a string into pieces of elements to form an array". The explode function in PHP enables us to break a string into smaller content with a break. This break is known as the delimiter.

    What is split in PHP?

    Definition and Usage. The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.