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: 52
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 11 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
David commented on 20 Aug 2008 -
Great site, thanks so much!

Awesome that you cared so much about helping people learn about explode() that you registered a TLD! :D
TimeTravel_0 commented on 27 Aug 2008 -
This site is now obsolete in the year 2021. The new code it goes as follows

PHPShift commented on 11 Nov 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?
_____________________

Easy peasy.

$string="A1 A2 A3 A4
B1 B2 B3 B4";

$string=explode("\n",$string);
for($i=0;$i<count($string);$i++)
{
$string[$i]=explode(" ",$string[$i]);
}

//print row 2, column 3
print $string[1][2];

No sweat. It's great or flat file databases.
Drew commented on 14 Nov 2008 -
Thanks this helped a ton.

ps. omg, a TLD for this?! wack.
pego81 commented on 18 Nov 2008 -
god damn!
a domain used just for this function! are you gonna take all the domains with php functions? very useless! cool!
PHPeditor commented on 3 Dec 2008 -
Is there a way to explode a word? I tried-

$ex_word= explode("",$word);

But it did not work...
dogmatic commented on 10 Dec 2008 -
i cant belive someone has this domain name... well they only need another 700 odd and all the php functions would be on their own domain name :)
YASH commented on 23 Dec 2008 -
Nice and easy...thanx
Austin commented on 13 Jan 2009 -
I, for one, am glad the TLD exists. As it has help me solve a problem that this newbie php coded had.

So, I am sharing a small snippet of code that may just help somebody else:

$num = "1,2,3,4,5,6";
$numarray = explode(",",$num);
for ($i = 0; $i < count($numarray); $i ){
echo "number = ".$numarray[$i]."";
}

Thanks again for this great page.
opm commented on 23 Jan 2009 -
not possible to explode a word with explode()
but heres another way:

$word="someword";
for($hlp=0;$hlp&lt;strlen($word);$hlp )
{
// here you can do what you want with each letter
echo $word{$hlp};
}
me commented on 14 Feb 2009 -
Like a lot of ppl above - OMG! a TLD just for one page?!
styts commented on 2 Apr 2009 -
I hope the author gets as much traffic as to justify the domain purchase.
Aubrey commented on 17 Apr 2009 -
So? There is a whole site dedicated to one command!
SO WHAT!!!
Its information!
Its USEFUL information!
Isn't that what the internet is all about?
Thanks - love it
mr commented on 4 May 2009 -
i dont get explode at all same with implode, i dont see what purpose it has. Anyone be kind to explain?
Ankit Bhatti commented on 7 May 2009 -
I just want to explode on word means i want to explode "test123" how can i get "t", "e", "s", "t", "1", "2", "3"??? If you have any idea give me that.

Thank you.
wtf commented on 10 May 2009 -
there's loads of these mini-sites:

phpsubstr.com
phpforeach.com
phpexplode.com
...
Vel commented on 15 Jun 2009 -
hey..i was wondering if there's a way to explode a quotation mark. I don't think explode(""", $string) works.=/
Booby_Bob commented on 2 Aug 2009 -
One page about exploding in PHP for one whole domain. That's great and all, but, why? The are no ads or anything, so that ain't the reason. Are you putting viruses on my computer as I type this? Now, that might be it...still I got the answer to what I was looking for, so thank you for that.
Colin commented on 1 Sep 2009 -
What happens if you explode a string that doesn't have the character you specify?

@mr, 3 posts up... I use it for storing arrays in a MQSQL database. Implode your array, store in the db as a string, pull it back out and explode into an array again.
Arthur commented on 17 Oct 2009 -
This is the most helpful site I've ever found on the net! And I'm not being sarcastic! Thank you So much!
Jayne commented on 26 Oct 2009 -
Can anyone suggest the code to get the data from a .txt file with data separated with semi-colons. I'm extremely new to this please take it from the top for me.

PS I love the TLD - put some advertising on it and make some money
big paulie commented on 1 Nov 2009 -
You rock!

Nice to see someone actually doing something for others, just for the sake of doing it.
DuG commented on 18 Nov 2009 -
How would one go about putting this type of delmited list into an array using explode()?

*value1*value2*value3*

Note: there are always beginning and trailing delimiters.

Thanks!
Raj commented on 20 Feb 2010 -
first in search result... got the solution Thanks!
Jowkss commented on 13 Mar 2010 -
explode in "#" ?
ish commented on 17 Mar 2010 -
is there any way to use a single space as seperator
for eg $r="Hi How are you";
explode(" ",$r);
will the above code works??
Athaur Rahman Najeeb commented on 22 Mar 2010 -
OMG .... A domain name just for this ? ... Great ...
Jimbob commented on 23 Mar 2010 -
@ish That will work
Brian commented on 6 Apr 2010 -
This isn't a TLD. com, org and net are top-level domains.

phpexplode is a second-level domain to the first-level com.
df commented on 22 Apr 2010 -
it is not complete you have to give some example.
with some single print not an array. thanks
Sky commented on 26 Apr 2010 -
WOW! I just found most useful site in the internet! Muahahahah :D Gratz for author! :D
Jenner commented on 27 Apr 2010 -
This is awesome. Thanks dude.
Chris O. commented on 2 Sep 2010 -
Love it! You should add a link to the explode() manual page on www.php.net for further reading too. Thanks for this service.
Dhanraj commented on 27 Sep 2010 -
its helpful for meee
thanks dude
seeman commented on 25 Feb 2011 -
thanks you