I created an easy to use API to help businesses do incredible things with AI.
PHP

The essentials of explode() in PHP

Benjamin Crozat
Published on Nov 8, 2023 0 comments Edit on GitHub
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() 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.

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -