Add comma to all valules except the last one?

Can anyone help me to to get this show like this:

value1, value2, value3

I mean not to add comma after the last value?

$explode = explode(", ",$fortag['tags']);
            $tagspostbit = "<div class='systemtag'>";
            $counttag = 0;
            foreach($explode as $tag)
            {
                $tagspostbit .= "<a href=\"tags/".$tag.".html\">".$tag."</a>, ";
                ++$counttag;
            }
            $tagspostbit .= "</div>";

You’re using the explode function already. implode does the opposite. Exactly what you want: http://php.net/function.implode

Instead of concatenating your tags into a string, add them to an array and use implode() to join them into a string at the end:

$explode = explode(", ", $fortag['tags']);
$counttag = 0;

$links = array();
foreach ($explode as $tag) {
    $links[] = "<a href=\"tags/$tag.html\">$tag</a>";
    ++$counttag;
}

$tagspostbit = '<div class="systemtag">' . implode(', ', $links) . '</div>';
1 Like

I think what you’re looking for is to use rtrim() before the

$tagspostbit .= "</div>";

And implode would work fine too. Up to you.

Though I’m a bit confused as to how that would look as link text

Guys thank yo very much but this reply is the best thanks again!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.