问题
let's say my code is.
<?php
$var = "1,2,3,4,5,6,";
$var_explode = explode(',', $var);
foreach ($var_explode as $number)
{
echo "$number test";
}
?>
And when it echos, it goes like, 1 test, 2 test, 3 test, 4 test, 5 test, 6 test, test. The last one is un-needed, I know its caused because I have a comma after the 6 in my variable, but I need that comma there, not going to remove it. Thanks!
回答1:
You can use trim($var, ",")
to remove the last comma when passing the string to the explode.
$var_explode = explode(',', trim($var, ","));
回答2:
<?php
$var = "1,2,3,4,5,6,";
$var_explode = explode(',', $var);
array_pop($var_explode); // removes last
foreach ($var_explode as $number)
{
echo "$number test";
}
?>
来源:https://stackoverflow.com/questions/20274884/ignore-last-data-explode-is-taking-php