PHP explode function explained in plain English

Updated: 29-Feb-2024 / Tags: PHP Lang Reference / Views: 237 - Author: George

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(string $delimiter, string $string, [int $limit]): array
  • $delimiter: Specifies the character or characters upon which to split the string. This can be a single character, such as a comma ,, or a multi-character delimiter, such as a space ' '.
  • $string: The string to be split into an array of substrings.
  • $limit (optional): Specifies the maximum number of elements in the resulting array. If provided, the function will stop splitting the string after the specified number of elements have been added to the array. If not provided, the function will split the entire string.
  • The function returns an array containing the split substrings.

Example

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.

$string = "George,38,male,Greece";
$array = explode(",", $string);
print_r($array);
	

If we run the function, we will get an array that holds the pieces

array(
  0 => 'George',
  1 => '38',
  2 => 'male',
  3 => 'Greece',
)

Limitation

You can also provide an optional $limit parameter to limit the number of elements in the resulting array. For example, setting $limit = 2 would result in the function splitting the string into a maximum of two elements, as shown below

$string = "George,38,male,Greece";
$limit = 2;				
$array = explode(",", $string, $limit);
print_r($array);
			

We will get the following result.

array = (
  0 => 'George',
  1 => '38,male,Greece',
);

By setting the limit argument to 2, we see that the string is split in two parts.

Real World Examples

Let's see where we can use the explode() function in our projects.

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

The explode function is a very useful tool in the applications that we write. In this tutorial, we learned the basics of the explode function and saw a few real-world scenarios on how to use it.

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

If you like to say thanks, you can buy me a coffee.

Buy me a coffee with paypal

Comment section

You can leave a comment, it will help me a lot.

Or you can just say hi. 😉