String manipulation is one of the most common tasks you'll undertake as a PHP developer, and splitting a string is no exception to that rule. Taking a string and separating out the different parts based on a particular type of character is all achievable using PHP's explode function. Our senior PHP developers will show you how it works, breaking down each parameter with easy-to-follow code examples, which you can copy and paste into your PHP project. Let's explore what the PHP explode is, and how it works.
PHP Explode Function
The PHP explode function, which makes up many parts of the string manipulation function list, is a useful and extremely common string function in PHP. Explode is great because you can split (hence explode) a string by any character, such as numbers, letters, or a comma. The explode function is particularly useful when you have multiple occurrences of the same character in the string that you are exploding. The opposite of the implode function in PHP, let's explore the explode function parameters.
The first required parameter explode accepts is the string separator. This is the character that you want to explode by. Next is the string you are exploding, which is also a required parameter. Finally, and optionally is the integer limit parameter. By default, PHP will set this to the predefined constant PHP_INT_MAX, which is the value that's been set prior to you running this command. The PHP_INT_MAX constant is normally the largest integer supported by your system or in other words, the build of PHP running the script. Systems running either 32-bit or 64-bit will have different values, with 64-bit systems running much larger maximums. After the explode function is complete, it will return an array of the results. The array will contain the inputted string, split by the separator you provided in the first param.
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
An important note to make about applications running PHP 8 or higher. The explode function has always been strict about the order of the separator and the string. This is unlike the implode function, where either way worked. Since PHP 7.4, passing the separator after the array was deprecated, and since PHP 8, this is no longer supported.
Since PHP 8, if you pass an empty string as the separator, PHP will throw a "ValueError", unlike prior to PHP 8, where it returned false.
Using PHP to Split a String by Character
Let's explore with PHP code examples how to split a string by a character, using the PHP explode function. Taking the below code example, you can see that we want to split the provided string by a backslash. Because our provided string does contain this character, the function will return an array of the results. When using another predefined PHP function, print_r, we can see the results of the explode function. We're left with two array items, 0 and 1. We're left with two items because we've cut the string by two because we've found one backslash. The function will return as many keys as it characters it finds.
$split = explode('/', 'Split this string / by the slash');
print_r($split);
# Outputs
Array
(
[0] => Split this string
[1] => by the slash
)
Using PHP to Cut a String
Splitting a string where multiple characters exist is also achievable when using the explode function. Let's explore what happens if you cut a string with PHP that contains multiple charters that make the separator. A perfect example of this is splitting up an IP address. IP addresses (IP4 in this case) are made up of numbers and periods (full stops). If we pass as the separator a full stop and an IP address, we'll get returned an array with multiple keys. When printing out the array, we see that it returns back 4 keys. As predicted, that's because there are four full stops in the provided IP address (127.0.01), which in case you didn't know, is the localhost IP.
$split = explode('.', '127.0.0.1');
print_r($split);
# Outputs
Array
(
[0] => 127
[1] => 0
[2] => 0
[3] => 1
)
Split String By Comma
Arguably the most common feature of the PHP explode function is to split a string by a comma. Let's explore how you can do this in your PHP application with a working code example that you can copy into your project. First, you must provide parameter one with a comma ",". Next, you must provide the string that you are looking to split. And finally, PHP will provide you with an array of results, in this case, it's a two-keyed array splitting "50,99" by the comma, resulting in 50 and 99.
$split = explode(',', '50,99');
print_r($split);
# Outputs
Array
(
[0] => 50
[1] => 99
)
Conclusion
Using the explode and implode functions can be a great way to work with strings in your project.
- Remember the parameter is important in order to get the correct results
- Provide any character as the explode separator
- Make sure you always pass strings into the function to avoid errors