00:00
00:00
Newgrounds Background Image Theme

Blackfog105 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

PHP: Explode and implode functions

12,930 Views | 6 Replies
New Topic Respond to this Topic

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.

Response to PHP: Explode and implode functions 2005-11-14 21:25:46


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


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to PHP: Explode and implode functions 2005-11-16 15:22:04


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

Response to PHP: Explode and implode functions 2006-01-06 16:19:17


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.


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to PHP: Explode and implode functions 2006-01-06 17:53:25


Its interesing...

Response to PHP: Explode and implode functions 2006-01-08 16:17:43


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?

Response to PHP: Explode and implode functions 2006-01-08 23:00:50


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.