Be a Supporter!

PHP: Explode and implode functions

  • 9,795 Views
  • 6 Replies
New Topic Respond to this Topic
DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
PHP: Explode and implode functions Nov. 14th, 2005 @ 08:57 PM Reply

PHP: Main

PHP: Using explode and implode functions

Note: Before reading this tutorial you should have basic knowledge of PHP.

I use PHP a lot. I find that when doing a large project one of the PHP functions that comes in handy the most it the explode function. The explode function allows you to break up a string into an array based on a seperator character. In this tutorial you will learn what the explode function can be used for, and how to use it. Also, I will go into using the opposite of the explode function, implode.

Chapter 1 :: A small explode example

To give you a better idea of what explode can do, lets write an example script that shows explode at the lowest level.

Code:

<?php

// A string variable of my favorite songs in order from favorite to least favorite!

$favorite_songs = 'Revolution, American Idiot, Bohemian Rhapsody, Ooh La La, Take Me Home Country Roads';

// Explode the $favorite _colors string

$favorite_songs = explode(',', $favorite_songs);

// Output the favorite color!

echo 'My favorite song is: ' . $favorite_songs[0];
?>

Now lets break down the script for a better understanding.

The first part of code is:

$favorite_songs = 'Revolution, American Idiot, Bohemian Rhapsody, Ooh La La, Take Me Home Country Roads';

That line simply declares our $favorite_songs variable, and we assign a string value of my favorite songs seperated by a comma to it.

Next we have:

$favorite_songs = explode(',', $favorite_songs);

That line is very important, as it is where we use the explode function. You'll see we give the explode function two parameters. The first one is the character we want to divide the string up on. In this case, that is a comma because my favorite songs are seperated by commas. The second is the string variable we want to use, which in this case is $favorite_songs. After it explodes the string, it converts $favorite_songs into an array whose each element is one of my favorite songs.

The last line reads:

echo 'My favorite song is: ' . $favorite_songs[0];

This line is simple. We first output "My favorite song it". Then, we echo the first array element, 0, in the array $favorite_songs. Explode orders the elements in the array in the order that they were found in the string. So $favorite_songs[0] would be my favorite song, $favorite_songs[1] would be my second favorite and so on.

Chapter 2 :: A slightly more complex explode script

In this example, I want to make a string of my classes during the school day, and write out some info on it. I'll comment within the script for this example because it is longer.

Code:

<?php

// Make our string variable, filled with my daily classes, seperated by commas

$my_classes = 'Business Law, Social Studies, English, Spanish, Lunch, Math, Gym/Sience, Sience, C++';

// Explode our class string

$my_classes = explode(',', $my_classes);

// Count how many classes I have a day for later use

$number_of_classes = count($my_classes);

// Lets write out some info!

echo 'First period I have ' . $my_classes[0] . ', and last period I have ' . $my_classes[$number_of_classes - 1] . '<br/>';
echo 'I have ' . $number_of_classes . ' every day.';
?>

Chapter 3 :: Using implode

I will go over the implode function now. Implode takes an array, and makes a string with each array element seperated by a dilimiter you specify.

Here's a quick example:

Code:

<?php

// Define an array of thinks I like to do

$things_i_like = array('Program', 'Watch TV', 'Play Tennis', 'Watch the Yankees during the season');

// Make a variable that will hold my sentance

$my_sentance = 'I like to ';

// Implode $things_i_like to make a readable sentance. It will insert and between each of the things I like to do.

$things_i_like = implode(' and ', $things_i_like);

// Output our sentance

echo $my_sentance . $things_i_like;
?>

I hope you learned a lot from this tutorial. I hope you see how usefull the explode an implode functions can be. If you have any questions, feel free to ask them right in this thread.


BBS Signature
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to PHP: Explode and implode functions Nov. 14th, 2005 @ 09:25 PM Reply

If you want to use the explode() function but pass a regular expression you can use the preg_split() function which explodes the string based on a regular expression passed to it.

www.php.net/preg-split

elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to PHP: Explode and implode functions Nov. 16th, 2005 @ 03:22 PM Reply

What do you actually want to do?


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to PHP: Explode and implode functions Jan. 6th, 2006 @ 04:19 PM Reply

At 11/16/05 03:22 PM, elbekko wrote: What do you actually want to do?

Who are you asking? No one has asked a question that I know of here.

On another note:
I overlooked this the first time I replied; however, I noticed your string has a comman (,) then a space ( ) in it. I would recommend using this instead:

$array = explode(', ', $string);

That way you don't have the extra space. Even though HTML doesn't allow double spacing like that, it still makes it more unprofessional.

Rinkul
Rinkul
  • Member since: Sep. 10, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to PHP: Explode and implode functions Jan. 6th, 2006 @ 05:53 PM Reply

Its interesing...

PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to PHP: Explode and implode functions Jan. 8th, 2006 @ 04:17 PM Reply

At 1/8/06 04:12 PM, Les_dawson wrote: I was helping on Newgrounds when some cunt [BLANKED] my [BLANK].

ANSWER

You do realise you're a fucking idiot, don't you?

Why would anyone click that link?

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to PHP: Explode and implode functions Jan. 8th, 2006 @ 11:00 PM Reply

I use implode when it comes to error handling... for example:
if ( strlen($pass) < 4 ) $error[] = 'You need to enter a minimum of 4 characters in the password field';
if ( $pass != $confirmed ) $error[] = 'The password you entered does not match the confirmed password';

if ( is_array ( $error ) ) {
echo 'The following errors occured:<br>' . implode ( '<br>', $error );
} else {
// do form processing stuff
}

Just handy for outputting multiple errors i guess.