# Trim a trailing comma

**URL:** <https://forum.getkirby.com/t/trim-a-trailing-comma/955>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 30, 2015, 12:10pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955 "2015-04-30T12:10:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bogus](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bogus/32/484_2.png) [@bogus](https://forum.getkirby.com/u/bogus)\
**Post date:** [April 30, 2015, 12:10pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/1 "2015-04-30T12:10:57Z")

</div>

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

```
rtrim

```

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

Anyone up for a hint?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 30, 2015, 12:27pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/2 "2015-04-30T12:27:31Z")

</div>

Yeah, instead of `strsplit` use the Kirby method:

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

```

[http://getkirby.com/docs/cheatsheet/field-methods/split](http://getkirby.com/docs/cheatsheet/field-methods/split)

---

<div class="post-metadata">

**Author:** ![bogus](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bogus/32/484_2.png) [@bogus](https://forum.getkirby.com/u/bogus)\
**Post date:** [April 30, 2015, 1:03pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/3 "2015-04-30T13:03:10Z")

</div>

mh, it now trims away ALL commas. ☹

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 30, 2015, 1:13pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/4 "2015-04-30T13:13:38Z")

</div>

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
<?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:

```css
.taglist:not(:last-child):after{
    content: ",";
}

```

---

<div class="post-metadata">

**Author:** ![bogus](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bogus/32/484_2.png) [@bogus](https://forum.getkirby.com/u/bogus)\
**Post date:** [April 30, 2015, 1:37pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/5 "2015-04-30T13:37:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 30, 2015, 1:44pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/6 "2015-04-30T13:44:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bogus](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bogus/32/484_2.png) [@bogus](https://forum.getkirby.com/u/bogus)\
**Post date:** [April 30, 2015, 3:33pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/7 "2015-04-30T15:33:09Z")

</div>

okay, that seems legit.

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

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:17pm UTC](https://forum.getkirby.com/t/trim-a-trailing-comma/955/8 "2025-02-06T17:17:27Z")

</div>


