PHP: explode()

Explode() is designed to split a given string in to pieces at a defined point (another string).

PHP Explode() Prototype

array explode ( string separator, string string [, int limit] )

Array: This array will contain each piece of the string after it has been split.

Separator: Whenever this string is encountered in the target string, the target string will be split into two pieces, using the separator as the middle point. The separator itself is discarded.

Limit: This is optional. After this many splits have been done, the rest of the string is placed in the last element of the array. Alternatively, if this is a negative number, e.g -x, then all but the last x splits will be performed.

How to Use Explode() in PHP

Let's say we have a list of names, each separated by a ""

$names = "Markus;Nigel;David";

To use these names in a meaningful way, we should first separate them into an array ($namearray), using explode():

$namearray = explode(";", $names);

The end result:

$namearray = Array ( [0] => Markus [1] => Nigel [2] => David )
Leave a comment
Name:

Comment:

 
Total comments: 17
David commented on 12 Jan 2007 -
Someone has a TLD just for this? Cool! Thanks for the help.
Eric commented on 12 Feb 2007 -
Just thought I'd tell you I stopped by your site, and it helped me a little.
yuval commented on 17 Feb 2007 -
nice! great!

it should be
$namearray = explode(";", $names);
and not
$namearray = explode($names, ";");

!!!
Ed Poor commented on 26 May 2007 -
Is it possible to split on the TAB character? Suppose I have:

$data = "john\tboston"; // I assume \t inserts a TAB character

I want to get $name = "john" and $city = "boston"
Martin commented on 29 May 2007 -
A domain just for this! Do this for all PHP functions and you are guranteed crazy traffic
d commented on 2 Jun 2007 -
Is it possible to split on the TAB character? Suppose I have:
$data = "john\tboston"; // I assume \t inserts a TAB character
I want to get $name = "john" and $city = "boston"

answer: write
list($name,$city) = explode("\t",$data)
jordan commented on 3 Jan 2008 -
Can I have more than one seperator like
$var=explode(" "|"/n",$text);
or something?
abcd commented on 27 Mar 2008 -
good one plz tell me the space provide by above is 4 ad or link
yym commented on 29 Apr 2008 -
i want to exploade the value and after exploding the values is store in variable.pls help me.
nerd commented on 3 Jun 2008 -
Lol why dedicate whole domain for this ? :D
J commented on 7 Jun 2008 -
Wow funny you have a site just for this as your upfront. At least it helps people. Thanks
tnl2k7 commented on 9 Jun 2008 -
This is mad, a TLD just for this? I mean, there's typos on this and all lol.....
amit commented on 12 Jun 2008 -
means string is convert in array from
ok
Matt commented on 25 Jun 2008 -
Yes, you can use a tab. Hey, web author. You should have a way that people can subscribe to updates on the comments for this page. Thanks for the site.

$arr = explode("t", $line);
Dj commented on 29 Jun 2008 -
WTF !! http://phpexplode.com/ just for a small tut??
Now ive seen everythign
Dz commented on 28 Jul 2008 -
Hi,

how about if i hv data smthg like this (from a file, separated by tab and n):
A1 A2 A3 A4
B1 B2 B3 B4

how to explode into 2-dimensional array?
chester commented on 1 Aug 2008 -
corny but you gotta love it..XD