what is explode function in php?

almostbob commented: RTFM read the effing manual -3

Dani AI

Generated

Short answer: explode() splits a string into pieces using a fixed string delimiter and returns those pieces as an indexed array. The delimiter (the string to split on) must be the first argument, the input string the second, and there’s an optional third limit argument that controls how many pieces you get. This is the behavior most answers here point to and what the docs show. (php.net)

Important edge cases and version notes to watch for (these bite people who read an old post and assume nothing changed): if the delimiter is not found you get an array with the original string as the single element; the limit parameter has special rules (positive = max N parts with last containing the rest, negative = omit last N parts, zero is treated like 1). Since PHP 8.0 passing an empty string as the delimiter now throws a ValueError (older PHP versions returned false or warned). If code may run on different PHP versions, check for an empty delimiter before calling explode(). (php.net)

Practical tips you can apply right away:

  • Use the limit argument to avoid accidental extra pieces (e.g., split into at most 2 parts to separate a key/value pair).
  • Clean results with array_map('trim', ...) and array_filter(...) to remove unwanted whitespace or empty entries.
  • If you need regex splitting, use preg_split(); explode() is faster for simple fixed delimiters.
  • For CSV-style data, prefer str_getcsv() or fgetcsv() rather than explode(',') — CSV needs enclosure/escape handling.

Example patterns (not used earlier in the thread):

list($key, $value) = explode('=', $pair, 2);
$values = array_filter(array_map('trim', explode(',', $csvLine)));

See the PHP docs and community writeups for the small semantics above (limit rules, empty-delimiter change) and the split/preg_split history noted by others here (thanks to , and for the pointers). (php.net)

Recommended Answers

All 6 Replies

You have all explained on this link, there you have a lot of examples that will show you everything.
Basicly it is a function that split sentence (any string) into array of words (string), but you need to send a request how to split that sentence.
For example:
"What + are + you + doing?"
For split u use: " + ";
You will get array like this:
Position 0 = "What";
Position 1 = "are";
Position 2 = "you";
Position 3 = "doing?";
Array start from 0 if you wonder what it is position 0.
Regards, Mike.

To add to what Mike has said, I' d like to share the syntax with an example-
$a=explode('2014-12-20','-');

the code snippet would return an array assigned to variable $a as follows:

$a[0]="2014", $a[1]="12", $a[2]="20"

To add to what JoyBh said, I'd like to share the correction. ;)

explode('-', '2014-12-20');

// returns

Array
(
    [0] => "2014",
    [1] => "12",
    [2] => "20"
)

Syntax is explode([delimiter], [string]);

To add more... explode() is the newer/safer version of split(). However, split() has been deprecated since version 5.3.0.

Spoiler alert
Entirely ridiculous post follows

Explode() is a construct developed for the lemmings game, it makes the lemming explode to blow a hole through the wall or floor
similarly
float() is a construct developed for the lemmings game, it makes the lemming move slowly down,

It is the silly season
commented: haha! +13
commented: ridiculous answer to ridiculous question. OP deserves to be ridiculed +15

The PHP function explode lets you take a string and blow it up into smaller pieces. For example, if you had a sentence you could ask explode to use the sentence's spaces " " as dynamite and it would blow up the sentence into separate words, which would be stored in an array. The sentence "Hello, I would like to lose weight." would look like this after explode got done with it:

Hello,
I
would
like
to
lose
weight.

The dynamite (the space character) disappears, but the other stuff remains, but in pieces. With that abstract picture of the explode function in mind, lets take a look at how it really works.
The explode Function

The first argument that explode takes is the delimiter (our dynamite) which is used to blow up the second argument, the original string. explode returns an array of string pieces from the original and they are numbered in order, starting from 0. Lets take a phone number in the form ###-###-#### and use a hyphen "-" as our dynamite to split the string into three separate chunks.
PHP Code:

$rawPhoneNumber = "800-555-5555";

$phoneChunks = explode("-", $rawPhoneNumber);
echo "Raw Phone Number = $rawPhoneNumber <br />";
echo "First chunk = $phoneChunks[0]<br />";
echo "Second chunk = $phoneChunks[1]<br />";
echo "Third Chunk chunk = $phoneChunks[2]";

Display:
Raw Phone Number = 800-555-5555
First chunk = 800
Second chunk = 555
Third Chunk chunk = 5555

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.