Trim a trailing comma

hey all,

I display my tags using this code:

<?php foreach(str::split($page->services()) as $tag): ?>
<a href="<?php echo url('services:' . urlencode($tag)) ?>"><?php echo $tag; ?></a>, 
  <?php endforeach ?>

The tags show up in a row separated by commas. The trouble now is that behind the last tag there’s still a comma. I know somehow i can trim a comma off a string by using


but I can’t seem to find a way to integrate it into my code. will I need to put in an extra function? :frowning:

Anyone up for a hint?

Yeah, instead of strsplit use the Kirby method:

<?php $page->services()->split(',') ?>

mh, it now trims away ALL commas. :frowning:

Yes, I thought that was the idea? The comma is the delimiter where you separate the tags. If you want to add some commas afterwards, you can put them in again and the best way to approach this would be with CSS instead of adding the comma after the <a> tag, so if you add a class to your <a> tag

<?php foreach($page->services()->split(',') as $tag): ?>
<a class="taglist" href="<?php echo url('services:' . urlencode($tag)) ?>"><?php echo $tag; ?></a>
  <?php endforeach ?>

you can then address that in your css:

    content: ",";

1 Like

ah okay. I thought maybe it would be cleverer to display all commas by php and trim the last one with php too.

I don’t think you could easily trim the comma, if you add it in the html like above. If you put it in a span tag, you could then hide the last comma via css, but that would be ugly, too. Or you could count the tags and then only add the comma, if its not the last tag, but then again that would require quite a bit of unnecessary code.

okay, that seems legit.

Thanks for your help again. Your kirby knowledge is really stunning! :hushed: