The essentials of explode() in PHP

The essentials of explode() in PHP

Published
Nov 8, 2023
Written by
Benjamin Crozat
0
comments
2 minutes
read

PHP’s explode() function is a go-to solution for splitting strings into arrays using a specified delimiter.

Let’s say that you have a string of comma-separated words. You can use explode() to split the string into an array of individual values.

In this article, I will show you in-depth how to use it.

How does explode() work?

array explode(string $delimiter, string $string[, int $limit ])
  • $delimiter: The string boundary for splitting.
  • $string: The input string.
  • $limit: This parameter is optional and represents the maximum number of elements to return. A positive value sets the size; a negative value excludes the last segments; zero entails no limit. Honestly, I never used this one. 🤷‍♂️

A practical example for explode()

I honestly don’t know what to write here, because using explode() is straightforward!

Given a string of devices:

$devices = explode(
    ", ", 
    "apple tv, apple watch, imac, iphone, macbook pro"
);

// array(5) {
//   [0]=>
//   string(8) "apple tv"
//   [1]=>
//   string(11) "apple watch"
//   [2]=>
//   string(4) "imac"
//   [3]=>
//   string(6) "iphone"
//   [4]=>
//   string(11) "macbook pro"
// }
var_dump($devices);

The output is an array of individual devices. Now, you can store them in a database for example.

A few notes about explode()

Be cautious when choosing your delimiter for the explode() function. If you use a delimiter that doesn’t exist in the input string, explode() will simply return an array containing the entire original string as a single element. Always double-check your input to ensure you’re getting the expected results.

For tasks that require the reverse operation (converting an array back into a string) turn to PHP’s implode() function. And if you need to break down a string into individual characters, str_split() is the function that offers that fine-grained control.

About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub