Difference between split() and explode() functions for String manipulation in PHP
Last Updated :
04 Nov, 2022
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);
?>
OutputString 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);
?>
OutputString 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);
?>
OutputString 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. |
Similar Reads
Difference between String.slice and String.substring in JavaScript These 2 functions are quite similar in their Syntax But are different in some cases. Let's see the difference between them. JavaScript slice() Method:This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted par
3 min read
What's the difference between
and
in PHP ? There are many types of line-ending characters that are used in PHP. They differ depending upon the operating system and editors using them. It's also based on how apps, libraries, protocols, and file formats deal with things. These characters are invisible. \n is used for the newline or linefeed, w
2 min read
Difference between array_merge() and array_combine() functions in PHP array_merge() Function: The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the e
3 min read
Explain some string functions of PHP In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols. For example, "Hello World!", "ID-34#90" etc. PHP also allows single quotes(' ') for defining a string. Every pro
7 min read
How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla
3 min read
How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a
2 min read