PHP: explode()
Explode() is designed to split a given string in to pieces at a defined point (another string).
PHP Explode() Prototype
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 ""
To use these names in a meaningful way, we should first separate them into an array ($namearray), using explode():
The end result:
Comment:
it should be
$namearray = explode(";", $names);
and not
$namearray = explode($names, ";");
!!!
$data = "john\tboston"; // I assume \t inserts a TAB character
I want to get $name = "john" and $city = "boston"
$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)
$var=explode(" "|"/n",$text);
or something?
ok
$arr = explode("t", $line);
Now ive seen everythign
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?
Awesome that you cared so much about helping people learn about explode() that you registered a TLD! :D
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.
ps. omg, a TLD for this?! wack.
a domain used just for this function! are you gonna take all the domains with php functions? very useless! cool!
$ex_word= explode("",$word);
But it did not work...
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.
but heres another way:
$word="someword";
for($hlp=0;$hlp<strlen($word);$hlp )
{
// here you can do what you want with each letter
echo $word{$hlp};
}
SO WHAT!!!
Its information!
Its USEFUL information!
Isn't that what the internet is all about?
Thanks - love it
Thank you.
phpsubstr.com
phpforeach.com
phpexplode.com
...
@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.
PS I love the TLD - put some advertising on it and make some money
Nice to see someone actually doing something for others, just for the sake of doing it.
*value1*value2*value3*
Note: there are always beginning and trailing delimiters.
Thanks!
for eg $r="Hi How are you";
explode(" ",$r);
will the above code works??
phpexplode is a second-level domain to the first-level com.
with some single print not an array. thanks
thanks dude