Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Explode() And Implode() Explained!, PHP - Beginner
Adrian
post May 8 2006, 11:38 AM
Post #1


Addicted Dot
Group Icon
Group: Moderator
Posts: 7,306
Joined: 26-September 04
Member No.: 763





explode() and implode() Explained! by Adrian
In 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,Jack

I 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


IPB
 
+Quote Post  Go to the top of the page
*D*
post May 15 2006, 11:11 AM
Post #2


Merry KissMoose
Group Icon
Group: Main Team
Posts: 15,988
Joined: 18-May 04
From: North Pole
Member No.: 2





flowers.gif Thanks for adding this wonderful addition Adrian, I did notice just got side tracked.
 
+Quote Post  Go to the top of the page
Memet
post Jun 2 2006, 11:11 AM
Post #3


New Member
Group Icon
Group: Member
Posts: 23
Joined: 21-April 06
Member No.: 10,871





Very well explained tutorial.
 
+Quote Post  Go to the top of the page
Arutha
post Jun 3 2006, 09:07 AM
Post #4


New Member
Group Icon
Group: *Premier*
Posts: 5
Joined: 31-August 05
Member No.: 6,596





a very nice tutorial, really detailed. N1 m8 smile.gif

Arutha


IPB
 
+Quote Post  Go to the top of the page
Adrian
post Jun 5 2006, 07:40 AM
Post #5


Addicted Dot
Group Icon
Group: Moderator
Posts: 7,306
Joined: 26-September 04
Member No.: 763





No problem guys/gals. Glad you liked it!


IPB
 
+Quote Post  Go to the top of the page

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 2nd December 2008 - 01:14 PM
Jordan Retro | Personal Loan | Loans | Secured Loans | Refinance