Open In App

Difference between split() and explode() functions for String manipulation in PHP

Improve
Improve
Like Article
Like
Save
Share
Report

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




<?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




<?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




<?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




<?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. Less 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.


Last Updated : 04 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads