Be a Supporter!

Php: Explode & Implode

  • 899 Views
  • 4 Replies
New Topic Respond to this Topic
Storm
Storm
  • Member since: May. 19, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Php: Explode & Implode 2006-11-16 12:51:02 Reply

PHP: Main

Ok, so today we shall learn about the two, quite similar, PHP functions - explode & implode. You should already have basic knowledge of an array.

Explode Function:

Explode basically turns a string into an array by splitting it. Confused? Lets look at an example.

Example On Explode

$explode_me = 'Hello World!';
$exploded = explode(' ', $explode_me);

The array 'exploded' would have two parts, one would be Hello, the other World!
Easy enough?

How To Write Explode

It is also worth looking at the way it is written:

array explode ( string delimiter, string string [, int limit] )

So, explode will return an array. The first part would be the seperator, which seperates the string into parts of the array. The second is the string you want to seperate.

Now, the third. This part is optional, this is a number that specifys how many
elements in the array you want. All the rest of the string will go in the last element. (This feature was added in PHP 4.0.1)

Other Details

- If the 'string delimiter' (the seperator) is empty, ie '', explode will return false.

- If the delimiter is not inside the string, it will return an array containing the string.

- If the limit is negative, (only in PHP 5.1.0) it will work as the opposite of the normal limit.

- Note that the order of the explode funtion must be delimiter before the string, unlike implode.

Implode Function:

Implode is the exact opposite of explode. Instead of taking a string and turning it into an array, we take an array and 'glue' it together. Lets look at an example.

Example On Implode

$parts = array('1', '2', '3');
$num = implode(', ', $parts);

$num would be a string, and would equal 1, 2, 3.

How To Write Explode

string implode ( string glue, array pieces )

This is about the same as explode, except there is no limit. Also, if you have PHP 4.3.0, the glue parameter is optional as it will be defaulted as ''. I have an example on that just incase you're confused.

$array = array('stor', 'm r', 'ules!');
$itstrue = implode(, $array); // storm rules!

_____________________________________________

Thats about it for explode and implode. Feel free to add, change or comment on this tutorial.

-- Storm

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Member
Level 51
Blank Slate
Response to Php: Explode & Implode 2006-11-16 12:53:46 Reply

What's the difference with Dfox's tutorial?

Jordan
Jordan
  • Member since: Apr. 23, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Php: Explode & Implode 2006-11-16 13:11:40 Reply

A good tutorial, but very much the same as DFox's implode and explode tutorial.

Also, i noticed some of what you said was copied directly from php.net. : /

Next time you should look to see if your idea of a tutorial has already been made, so don't feel sad if this doesn't get included.

Storm
Storm
  • Member since: May. 19, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Php: Explode & Implode 2006-11-16 14:00:19 Reply

At 11/16/06 12:53 PM, Zendra wrote: What's the difference with Dfox's tutorial?

I explained much more about the actual function parts, as well as the limit number.

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Explode & Implode 2006-11-16 19:15:40 Reply

Well, it is pretty much the same as mine, but I've said before that I really don't mind one subject being written about more than once as some people learn better from different teachers. I think it's a good tutorial.

With that said, Jordan, thanks for pointing that out. I looked on PHP.net, and I only saw some of the notes taken. What exactly were you referring to?

I have no problem with you writing a tutorial on the same subject, but I would have a huge problem if some of it was ripped.

In the future, I think we should try to avoid tutorials on the exact same subject as I think it might confuse people, but this will be included.


BBS Signature