PHP explode() is a built-in function that is used to split a string into a string array on boundaries formed by the separator. For example, if you have a PHP string that contains IDs with the comma operator, you can split the string into an array of IDs with the explode() function.
The PHP explode() makes it easy to extract and work with specific parts of a string to an array, enabling developers to process and handle data more efficiently.
In this tutorial, we are going to discuss splitting a string into an array of strings with the explode() function.
What is the explode() function in PHP?
The explode() in PHP is used to split a string into an array of substrings based on a defined delimiter (separator). Its basic syntax is as follows:
explode(separator, string, limit = PHP_INT_MAX);
separator | Required | The boundary string. The “separator” parameter cannot be an empty string. |
string | Required | The input string |
limit | Optional | If the limit is positive, the returned array will contain a maximum of limit elements with the last element containing the rest of the string. If the limit parameter is negative, all components except the last limit are returned. If the limit parameter is zero, then this is treated as 1. |
Returned Value | Returns an array of strings |
In PHP 8.0.0 and above, if the separator parameter is set to an empty string (”), it will throw ValueError. In earlier versions of the PHP, the explode function returns false instead of the array.
This function is binary-safe.
$cars = "Ferrari Benz BMW Volvo Toyota";
var_dump(explode(" ", $cars));
Output
array (size=5)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
4 => string 'Toyota' (length=6)
This example separates the string into an array by space.
You can set the separator of any separator character in the PHP explode() function to separate the string to an array by the character.
$cars = "Ferrari,Benz,BMW,Volvo,Toyota";
var_dump(explode(",", $cars));
Output
array (size=5)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
3 => string 'Volvo' (length=5)
4 => string 'Toyota' (length=6)
PHP explode function with the limit parameter
$cars = "Ferrari,Benz,BMW,Volvo,Toyota";
var_dump(explode(',', $cars, 0));
Output
array (size=1)
0 => string 'Ferrari,Benz,BMW,Volvo,Toyota' (length=29)
If you set the limit parameter to zero, the explode function will return the whole string as a PHP array.
$cars = "Ferrari,Benz,BMW,Volvo,Toyota";
var_dump(explode(',', $cars, 2));
Output
array (size=2)
0 => string 'Ferrari' (length=7)
1 => string 'Benz,BMW,Volvo,Toyota' (length=21)
If you set the limit with a positive number, the function will return the exploded string into an array with the entered number of elements. If you enter a number that is more than the separated elements, elements will be the maximum number of the separated elements
$cars = "Ferrari,Benz,BMW,Volvo,Toyota";
var_dump(explode(',', $cars, -2));
Output
array (size=3)
0 => string 'Ferrari' (length=7)
1 => string 'Benz' (length=4)
2 => string 'BMW' (length=3)
If you enter a negative number for the limit, the last items of the returned array will be ignored.
More about the function
The PHP explode function is incredibly versatile and finds application in a wide range of scenarios to separate a string to an array. Here are a few practical examples that demonstrate the power and usefulness of this function:
Parsing URLs: When working with URLs, developers often need to extract specific components, such as the domain, path, or query parameters.
Processing CSV Files: Comma Separated Values (CSV) files are commonly used for data storage and exchange. explode can be employed to split each line of a CSV file into an array, making it convenient to access and manipulate individual fields.
Handling User Input: PHP applications frequently require processing user input, such as form submissions. explode can be leveraged to split user-entered data into an array, facilitating validation and further processing.