explode() and implode() Explained! by AdrianIn this short guide I will be explaining both the explode function and implode function. Both are very simple functions but both can be usefully applied in a real world scenario.
explode()explode() is quite simple. Explode outputs an array of strings formed by spliting a string at the point of a seperator, it is also possible to limit the number of times this is done for each string.
Ok, lets say we want to split this date up into 3 parts. We know the the date we have will be in this format: dd/mm/yyyy so I want to split the string wherever there is a "/". This is simple to do with the following code:
CODE
<?php
$date = "23/09/2006";
$exploded = explode("/", $date);
echo $exploded[0]." : The Day<br>";
echo $exploded[1]." : The Month<br>";
echo $exploded[2]." : The Year<br>";
?>
This will output the following:
QUOTE
23 : The Day
09 : The Month
2006 : The Year
Explanation:$date = "23/09/2006"; - This is the variable which contains the string we want to split up. Note the /s.
$exploded = explode("/", $date); - This is the actual explode() function. When outputted it is an array so we need to give it a name which, in this case, is
$exploded. The / (forward slash) in quotes is what we want to seperate the string with, so for example, if we had written the date like this 23.09.2006 we would change this forward slash to a . (full stop). The second bit of information after the comma is the string we want to split so we are specifying the variable with the date in it.
Now the array has 3 pieces of information in it unless we put too many or too little /s in the date variable. The first piece, which PHP calls piece number 0 (because PHP starts counting at 0 not 1) is the day, the second piece of data is the month which PHP defines as piece number 1 and then the year is the 3rd piece of information is the year which PHP calls piece number 2.
CODE
echo $exploded[0]." : The Day<br>";
echo $exploded[1]." : The Month<br>";
echo $exploded[2]." : The Year<br>";
This simply prints the information I talk about in the above paragraph to the page.
To limit how much splitting the function does then you would change the explode funtion in this script to something like
$exploded = explode("/", $date, 2); - Note the number after the $date part, this tells PHP how many pieces you want, whatever hasn't been split up in the first pieces will all be put in the last piece, so this would output piece [0] being the day and piece [1] being the month and year in one as they appear in the origanal $date variable.
Right, thats explode() finished, now for...
implode()implode() simply does the reverse of explode(). Instead of being fed a string and outputting an array of strings (thats what explode() does) it gets fed an array of strings and outputs a variable.
So, for example, we are going to put an array of names together in a list of names instead of an array.
CODE
<?php
$names = array('John', 'Bob', 'Pete', 'Pat', 'Susie', 'Harry', 'Jill', 'Fred', 'Jack');
$names_list = implode(",", $names);
echo $names_list;
?>
$names = array('John', 'Bob', 'Pete', 'Pat', 'Susie', 'Harry', 'Jill', 'Fred', 'Jack'); - This just specifies an array called $names with a list of names in it.
$names_list = implode(",", $names); - This is the actual implode function. Implode outputs as a string so it needs to be inside a variable which I have called $names_list. The first comma in quotes is what we want to put between each piece of information we're getting from the array, the $names bit after the comma is saying which array we want to use.
echo $names_list; - Prints the list of names to the page, in this it outputs the following:
John,Bob,Pete,Pat,Susie,Harry,Jill,Fred,JackI hope you learnt something from that, although these may not seem like very useful applications for the 2 functions it is possible to use them very usefully, for example, say you had a huge database of comments with dates in the format suggested above (dd/mm/yyyy) but your upgrading your site and you want to display them in a tidyer way (e.g. 29th of September 2006) but don't want to have to go updating hundreds, possibly thousands of entries, well all you have to do is use the explode funtion to split them up then you can use PHP to write them out in a much nicer-looking way.
Regards, Adrian