Lower case for Tags

I’m categorising my blog posts with tags and displaying a tag cloud. I’m using urlencode but I also want my tag URLs to be lower case. Here’s my template code so far. I’ve done some searches but I’m struggling with generating the lower case URLs. What 's the best way of doing this?

<ul class="nav-list secondary">
        <?php foreach($tags as $tag): ?>
        <li>
          <a href="<?= url($page->url(), ['params' => ['tag' => urlencode($tag)]]) ?>">
            <?= html($tag) ?> (<?= $posts->filterBy('tags', $tag, ',')->count() ?>)

          </a>
        </li>
        <?php endforeach ?>
      </ul>
Str::lower($tag)

will give you lowercase tags. (Str::lower() | Kirby CMS)

You then need to filter by lowercase tags as well.

Many thanks. I’m filtering like this:

  $tags = $articles->pluck('tags', ',', true);
  sort($tags);

  if($tag = param('tag')) {
    $articles = $articles->filterBy('tags', urldecode($tag), ',');
  }

You mean lower case with the pluck method?

No, with the filter method. Because I suspect that if you have uppercase tags and filter by lowercase, it won’t work.

Instead of filterBy(), you would then have to use the filter() method which accepts a callback.

I’ve reached the limit of my PHP. Can you point me to an example? Thanks.

$articles = $articles->filter(fn ($article) => in_array(urldecode($tag), array_map('strtolower', $article->tags()->split(','))));

Or if you prefer the pre-7.4 syntax:

$articles = $articles->filter(function ($article) use($tag) {
  return in_array(urldecode($tag), array_map('strtolower', $article->tags()->split(',')));
});

OK, got it, all working here. Many thanks for your help. Much appreciated.