Explode takes a string and splits it up. You can split a string into an array as follows:
<?php
$mystring = "Fred,Bloggs,10 Fred's Road,Fredsville";
$myarray = explode(',' $mystring);
?>
What the above does is it splits the string wherever a ',' is found, so the resultant array would be as follows:
$myarray[0] = "Fred"
$myarray[1] = "Bloggs"
$myarray[2] = "10 Fred's Road"
$myarray[3] = "Fredsville"
You could also split it into a list of variables as follows:
<?php
$mystring1 = "Fred|Bloggs|10 Fred's Road|Fredsville";
list($first, $last, $road, $town) = explode('|' $mystring1);
?>
Which would give:
$first = "Fred"
$last = "Bloggs"
$road = "10 Fred's Road"
$town = "Fredsville"
You can reverse the effects of explode with implode. Working with the array of:
$myarray[0] = "Fred"
$myarray[1] = "Bloggs"
$myarray[2] = "10 Fred's Road"
$myarray[3] = "Fredsville"
We could put it back together with:
<?php
$mystring = implode(',', $myarray);
?>
And $mystring would once again be: "Fred,Bloggs,10 Fred's Road,Fredsville"
Be careful exploding an empty string as it will still create an array with a blank member. For example:
<?php
$mystring = "";
$myarray = explode(',' $mystring);
?>
Would still define:
$myarray[0] = ""
This may be of some consequence if you use the array at some point later. |