Page 1 of 1

Explode

Posted: Fri Sep 22, 2006 4:24 pm
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?

Posted: Fri Sep 22, 2006 4:44 pm
by feyd
explode() doesn't do anything with an array other than returning one.

Posted: Fri Sep 22, 2006 4:52 pm
by Luke
yea it accepts a string, and returns an array, buddy.

Posted: Sat Sep 23, 2006 12:00 am
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"
?>

Posted: Sat Sep 23, 2006 2:28 am
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