<p>Craft has a filter for "<a href="https://craftcms.com/docs/templating/filters#kebab">Kebab Case</a>", which I think is what you need. It strips out punctuation and replaces spaces with dashes. Useful if you need to format a field as a HTML attribute.</p>

<pre><code>{{ tag.name|kebab }}
</code></pre>

CC BY-SA 3.0
2015-12-03T06:08:12.860Z
Perfect answer. Worth noting that `kebab` didn't exist at the time this question was originally asked. It was added on **Jun 2, 2015**, when [Craft 2.4.2664](https://craftcms.com/changelog#build2664) was released.
Lindsey D
CC BY-SA 3.0
2015-12-03T01:09:05.507Z
2015-12-03T01:09:05.507Z
kmgdev
<p>I'm not sure if this feature is newer than this question but you can also say <code>{{ tag.slug }}</code> to get <code>apples-and-carrots</code>. All letters become lowercase as well so the result is more "scripting-friendly".</p>

CC BY-SA 3.0
2015-12-03T07:41:24.860Z
This should be the accepted answer.
carlcs
CC BY-SA 3.0
2015-05-13T19:33:12.960Z
2015-05-13T19:33:12.960Z
Andreas Linnert
<p>one more example with array parameter in replace method:</p>

<pre><code>{{ "apples and carrots"|replace({" ":"-"}) }}
{# outputs "apples-and-carrots" #}
</code></pre>

CC BY-SA 4.0
2019-12-05T08:22:27.627Z
2019-12-05T08:22:27.627Z
Faiyaz Alam
<p>There are multiple ways to solve this:</p>

<p><strong>Native:</strong></p>

<p><a href="http://twig.sensiolabs.org/doc/filters/url_encode.html">Twigs <code>url_encode</code> filter</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|url_encode }}
{# outputs "apples%20and%20carrots" #}
</code></pre>

<p><a href="http://twig.sensiolabs.org/doc/filters/replace.html">Twigs <code>replace</code> filter</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|replace(' ', '-') }}
{# outputs "apples-and-carrots" #}
</code></pre>

<p><strong>Plugins:</strong></p>

<p><a href="https://github.com/victor-in/Craft-TwigPCRE">Twig PCRE Filters - Plugin</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|preg_replace('/ /', '-') }}
{# outputs "apples-and-carrots" #}
</code></pre>

<p><a href="https://github.com/low/low_regex/">Low Regex - Plugin</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|regex('/ /', '-') }}
{# outputs "apples-and-carrots" #}
</code></pre>

<p><a href="https://gist.github.com/boboldehampsink/7354431">Slugify - Plugin</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|slugify }}
{# outputs "apples-and-carrots" #}
</code></pre>

CC BY-SA 3.0
2014-07-15T15:31:01.120Z
If you want to handle "apples & *!@#@! carrots" then you need the slugify plugin
Marion Newlevant
CC BY-SA 3.0
2014-07-15T13:39:36.433Z
2014-07-15T13:39:36.433Z
Victor
<p>I'm using tags to do some Javascripting.</p>

<p>Is there any way that I can tell Craft to remove whitespace from {{ tag.name }} if the tag.name has spaces (for example 'apples and carrots'), and add dashes ('apples-and-carrots') ?</p>

<tags>
Removing whitespace from {{ tag.name }}
# Removing whitespace from {{ tag.name }}

(Asked by: username_0 on 2014-07-15)

I'm using tags to do some Javascripting.

Is there any way that I can tell Craft to remove whitespace from {{ tag.name }} if the tag.name has spaces (for example 'apples and carrots'), and add dashes ('apples-and-carrots') ?

**Tags:** tags


---

## 4 answers

### Answer by username_6 on 2014-07-15. Score: 25 | Accepted answer

There are multiple ways to solve this:
Native:
Twigs url_encode filter:
{{ "apples and carrots"|url_encode }}
{# outputs "apples%20and%20carrots" #}

Twigs replace filter:
{{ "apples and carrots"|replace(' ', '-') }}
{# outputs "apples-and-carrots" #}

Plugins:
Twig PCRE Filters - Plugin:
{{ "apples and carrots"|preg_replace('/ /', '-') }}
{# outputs "apples-and-carrots" #}

Low Regex - Plugin:
{{ "apples and carrots"|regex('/ /', '-') }}
{# outputs "apples-and-carrots" #}

Slugify - Plugin:
{{ "apples and carrots"|slugify }}
{# outputs "apples-and-carrots" #}

> **Comments:**
> **username_7**: If you want to handle "apples & *!@#@! carrots" then you need the slugify plugin  

---

### Answer by username_1 on 2015-12-03. Score: 15

Craft has a filter for "Kebab Case", which I think is what you need. It strips out punctuation and replaces spaces with dashes. Useful if you need to format a field as a HTML attribute.
{{ tag.name|kebab }}

> **Comments:**
> **username_2**: Perfect answer. Worth noting that `kebab` didn't exist at the time this question was originally asked. It was added on **Jun 2, 2015**, when [Craft 2.4.2664](https://craftcms.com/changelog#build2664) was released.  

---

### Answer by username_3 on 2015-05-13. Score: 9

I'm not sure if this feature is newer than this question but you can also say `{{ tag.slug }}` to get `apples-and-carrots`. All letters become lowercase as well so the result is more "scripting-friendly".

> **Comments:**
> **username_4**: This should be the accepted answer.  

---

### Answer by username_5 on 2019-12-05. Score: 1

one more example with array parameter in replace method:
{{ "apples and carrots"|replace({" ":"-"}) }}
{# outputs "apples-and-carrots" #}


---

