How to decode url

I am trying to remove ‘%20’ from my filter tag URL’s as my code to add ‘active’ class isn’t working for tags with spaces

I have tried the below but none have worked

str_replace("%20", "-", $tag)

My code:

<?php $tags = $page->children()->listed()->pluck('servicecategory', ',', true); ?>
    <a class="p" href="<?= $page->url() ?>">all, </a>
    <span class="tag-links">
    <?php foreach($tags as $tag): ?>
      <a class="p tag <?= param('category') == $tag ? "active-tag" : '' ?>" href="/work/category:<?php echo str_replace("%20", "-", $tag); ?>">
        <span class=""><?= $tag ?></span>
<?php endforeach ?>

The browser will replace all the spaces in the tag with %20 again, as soon as a user clicks on the link. So the replace in the href is the wrong place. Try to remove those spaces in your conditional statement, and it might be better to use something like urldecode:

<?= urldecode(param('category')) == $tag ? 'active-tag' : '' ?>