Explode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Explode

Post by gkwhitworth »

Ok I am trying to grasp the "explode" idea. The PHP Manual states:

Code: Select all

<?php
// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *

?>
So what exactly is explode doing for you that you can't already do. Can't you already echo the items in an array as long as you know their number?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

explode() doesn't do anything with an array other than returning one.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yea it accepts a string, and returns an array, buddy.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

The manual stated more than just that example code... something along the lines of "Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string."

Code: Select all

<?
$myString = "hello world";
$myArray = explode(' ', $myString);

echo $myArray[1] . ', ' . $myArray[0]; // prints "world, hello"
?>
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

Yeah, I kinda put that together moments after I posted this thread when I realized that there were no commas between the words. Nor the words "array." I was six hours into work so I guess my mind was kinda PHP fried. Anyways, I am sorry if some of you thought I should close the thread, but I wanted some guru take on explode even though I had gotten the gist of it.

Thanks.

--
Greg
Post Reply