PHP explode function explained in plain English
Updated: 29-Oct-2022 | Tags: PHP Lang Reference | Views: 155
Introduction
Hi everyone, in this tutorial i am going to explain as simple as i can how the explode() php function works. We are gonna see real world examples on how we make use of the explode() function. But first let's go through some details about the function.
The function's syntax
The explode function takes three arguments.
explode("separator", "the string", $limit);
-
The first argument is the separator, and is is a string. With the separator we tell the function where to cut a given string.
Let's say we have the following string, "George,38,male,Greece", and let's use the comma (,) as the string's separator. This means that the function will cut the string at every comma (,) it finds.
explode(",", "George,38,male,Greece");
If we run the function, we will get an array that holds the pieces
array ( 0 => 'George', 1 => '38', 2 => 'male', 3 => 'Greece', )
Let's say now we have the same string, "George 38 male Greece", but without the commas. In this case we can use the white space between the words to split the string.
explode(" ", "George 38 male Greece");
If we run the function, we will get the same array.
array ( 0 => 'George', 1 => '38', 2 => 'male', 3 => 'Greece', )
When we use the space character to split a string, it is important that the length of the space is equal, both in the string and the separator.
Let's see an example where the separator's white space is 4 characters long and don't match any white space in the string.
explode(" ", "George 38 male Greece");
This will not split the string into words, but will create the array bellow.
array ( 0 => 'George 38 male Greece', )
We will see more examples later on, now let's go to the second argument.
-
The second argument is the string that we want to split.
The string can contain any character.
In example it can be a filepath "/var/www/html/my-project/index.html", or it can be a URL "https//digitalfox-tutorials.com", or just a phrase "Hi there i am a string".
But usually we deal with comma (,) separated keyword strings like: "html,css,javascript,php,mysql".
Usually the string is stored in a variable...
$phrase = "Hi there i am a string";
...and we use the variable as the second argument.
explode(" ", $phrase);
-
And with the third argument which is optional we can limit the pieces that we have in the returned array.
Let's set the limit argument to 2, and run the function.
$user = "George,38,male,Greece"; $arr = explode(",", $user, 2); print_r($arr);
We will get the following result.
$arr = ( 0 => 'George', 1 => '38,male,Greece', );
By setting the limit argument to two, we see that the string is splitted in two parts.
If we set the limit to -2...
$arr = explode(",", $user, -2);
...we will exclude the last two pieces.
$arr = ( 0 => 'George', 1 => '38', );
Play around with the limit and see the different results that you get.
That was the basics on how the explode() function works. Now let's see some real world application examples.
Get the query string from an URL
We can use the explode() function to get the query string out of an URL address.
Lets say we have the following URL string.
$url = "https//digitalfox/tutorial.php?title=some-title";
We know that every query string starts after a question mark. So let's split the URL using the question mark as a separator.
$arr = explode("?", $url);
This will give us the following array.
array (
0 => 'https//digitalfox/tutorial.php',
1 => 'title=some-title',
)
We see that the query string has a key of one 1 =>.
Now we can use the key to get the query string.
$query_string = $arr[1]; // out: 'title=some-title'
Using the explode function to get a file's extension
We can use the explode() function to get a file's extension. Lets say we have the following image name.
$image_name = "greenlights.png";
We can use the dot (.) as the separator and split the image name in two parts.
$arr = explode(".", $image_name);
This will return the following array.
array (
0 => 'greenlights',
1 => 'png',
)
In the first part we have the name, and in the second part we have the extension.
Next we are going to use the end() function to get the last item from the array, which in our case is the file's extension.
$ext = end($arr); // out: png
We are using the end() function, and not targeting the key to get the extension because there is a big change that a filename contains more than one dot (.). So using the end() function we will always be sure the we get the file's extension.
Converting a path to an array
Let's see how we can turn a filepath to an array. Let's say we have the following path.
$path = "var/www/html/digitalfox-tutorials";
This time we use the forward slash (/) as a separator to split the path
$arr = explode("/", $path);
This will return the following array.
array (
0 => 'var',
1 => 'www',
2 => 'html',
3 => 'digital-fox-tutorials',
)
Now we have every step of the path stored as an item inside the $arr array.
Creating a list from a string
Let's say we have the following image details in a string. We have the width, the height, the color, the type, and the size.
$image_details = "1200px,720px,true color,png,300kb";
We will use a list() function and pass-in as arguments the image's properties that we have separated in the string by commas.
list($image_width, $image_height, $color, $image_type, $image_size) =
explode(",", $image_details);
We will set the list() function equal to the array that the explode() function will return.
This means that every value from the array that the explode function returns, will be assigned to the list() function's arguments starting from the first one.
So the above code will return the below array.
array (
0 => '1200px',
1 => ' 720px',
2 => ' true color',
3 => ' png',
4 => ' 300kb',
)
But instead of targeting the keys to get the values it is now simpler to use the list() function's arguments.
echo $image_width; // out: 1200px
echo $image_type; // out: png
Summary
We saw the basics of the explode function and how to use the function in real world applications.
Thanks for reading, i hope you find the article helpful.
Please leave a comment if you find any error's, so i can update the page with the correct code.
Buy me a coffee
Tutorial Categories
Comment section
You can leave a comment, it will help me a lot.
Or you can just say hi. 😉