Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

The essentials of explode() in PHP

Table of contents:

The essentials of explode() in PHP

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() works?

array explode(string $delimiter, string $string[, int $limit ])
  • $delimiter: The string boundary for splitting.
  • $string: The input string.
  • $limit: This parameter is options and represents the maximum number of elements to return. A positive value set the size; A negative value exclude 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.

Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

PHP 8.3 is out, now! Here's what's new and changed. PHP 8.3 is out, now! Here's what's new and changed.

PHP 8.3 was released on November 23, 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Modified on Nov 23, 2023

PHP
20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

console.log() in PHP console.log() in PHP

Explore the world of PHP debugging with var_dump(), and Laravel's friendlier alternatives, dump() and dd(). Much charm, such useful!

Modified on Sep 6, 2023

PHP
Laravel interview questions and answers for 2023 Laravel interview questions and answers for 2023

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Modified on Sep 19, 2023 Audio available

Print an array with PHP (+ Laravel) Print an array with PHP (+ Laravel)

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP (and even Laravel-specific helpers).

Modified on Jun 24, 2023

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -