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.