MyBB Community Forums

Full Version: php explode problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Whats wrong with this code?

<?
$marlins=array("Castillo, "Pierre", "Delgado", "Cabrera");
$doit=explode(",",$marlins);

echo "<li>".$marlins;
?>
" in the wrong places. Try this:

<?
$marlins = "Castillo-|-Pierre-|-Delgado-|-Cabrera";
$doit = explode("-|-",$marlins);

echo "<ul>\n";
foreach($doit AS $point)
{
echo "<li>".$point."</li>\n";
}
echo "</ul>\n";
?>
You might have to put backslashs at those quotes around <li> And what's with the period before $marlins? If you do that, your script will think that it's a concatenation period with " . $marlins . " ...if you are trying to have a string concatenation then the proper way is:

echo "<li>" . $marlins;
If you want to stick with what you've wrote (I'm assuming you are turning an array into a list?)

<?
$marlins=array("Castillo, "Pierre", "Delgado", "Cabrera");
$doit=explode("</li>\n<li>",$marlins);

echo "<li>".$marlins . "</li>";
?>
decswxaqz Wrote:If you want to stick with what you've wrote (I'm assuming you are turning an array into a list?)

<?
$marlins=array("Castillo, "Pierre", "Delgado", "Cabrera");
$doit=explode("</li>\n<li>",$marlins);

echo "<li>".$marlins . "</li>";
?>

using implode() would be better for that if you're writing it all into different points on a list Wink
Cory Wrote:You might have to put backslashs at those quotes around <li> And what's with the period before $marlins? If you do that, your script will think that it's a concatenation period with " . $marlins . " ...if you are trying to have a string concatenation then the proper way is:

echo "<li>" . $marlins;

The original one was correct as well. Concatenate a string with a variable, you need a period (dot), but it doesn't matter if there's a space around it or not, it will still work as a concatenation operator. You'll get a parse error if you try to do: echo \"<li>\" . $marlins;
rage Wrote:Whats wrong with this code?

Explode is to separate a string into an array.
Implode is to make a string from an array.