Question

Hi i have this line of code below

list($tag1, $tag2, $tag3, $tag4, $tag5) = array_pad(explode(',', $new_shout_tags, 5), 5, "");

It, basically assigns no value to the elements in the list if there is nothing else in the $new_shout_tags. So basically if i have

$new_shout_tags = "asd, asdf, asdfg";

then list will be

$tag1 = "asd";
$tag2 = "asdf";
$tag3 = "asdfg";
$tag4 = "";
$tag5 = "";

Which is all good however if the user enters

$new_shout_tags = "asd, asdf, asdfg, asdaf, asdafa, afads, asdasfd";

Which is 7 elemnts for example then the explode creates this in the list

$tag1 = "asd";
$tag2 = "asdf";
$tag3 = "asdfg";
$tag4 = "asdaf";
$tag5 = "asdafa, afads, asdasfd";

How do i make it not include the last 2 or however many over 5 that the user has entered, just the first 5 only.

Thanks

Était-ce utile?

La solution

Use a garbage variable.

list($tag1, $tag2, $tag3, $tag4, $tag5, $garbage) =
    array_pad(explode(',', $new_shout_tags, 6), 6, "");

Autres conseils

this should work:

$v1=explode(',', $new_shout_tags); // with no limit
$v2=array_slice($v1,0,5);
$v3=array_pad($v2, 5, "");
list($tag1, $tag2, $tag3, $tag4, $tag5) = $v3
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top