<p>It was clearly documented on <a href="https://twig.symfony.com/doc/2.x/filters/url_encode.html" rel="nofollow noreferrer">Twig Official Documentation</a> that we can use <code>url_encode</code> filter to convert it to url friendly string.</p>

<p><strong>Example</strong></p>

<pre><code>{{ "path-seg*ment"|url_encode }}
{# outputs "path-seg%2Ament" #}

{{ "string with spaces"|url_encode }}
{# outputs "string%20with%20spaces" #}

{{ {'param': 'value', 'foo': 'bar'}|url_encode }}
{# outputs "param=value&amp;foo=bar" #}
</code></pre>

CC BY-SA 3.0
2017-08-16T18:01:45.970Z
2017-08-16T18:01:45.970Z
Jefry Dewangga
<p>I was about to ask this question, but realized that it's closely related to <strong><a href="https://craftcms.stackexchange.com/questions/184/is-it-possible-to-convert-string-to-uppercase">this question</a></strong>.</p>

<p>The answer, like in the above referenced question <a href="http://twig.sensiolabs.org/doc/filters/index.html" rel="noreferrer">lies with Twig filters</a>.</p>

<p>Specifically, in this case, one needs to use the <a href="http://twig.sensiolabs.org/doc/filters/url_encode.html" rel="noreferrer"><code>url_encode</code></a> filter to convert the text needed.</p>

<p><strong>Example</strong></p>

<pre><code>&lt;a href="http://www.linkedin.com/shareArticle?mini=true&amp;url={{ entry.url|url_encode }}&amp;title={{ entry.title|url_encode }}&amp;source=Xavier%20Creative%20House&amp;summary={{ entry.question|url_encode|striptags }}" target="_blank"&gt;
</code></pre>

<p>In any case, I hope that helps others with this. I know how easy it is to forget that the answers sometimes like over in the Twig docs, and not Craft’s.</p>

CC BY-SA 3.0
2014-07-14T22:35:05.907Z
2014-07-14T22:35:05.907Z
2017-04-13T12:51:00.200Z
Christopher Kennedy-Nuñez
<p>If I want to convert text into a URL friendly string for sharing text in a social media link, how would I go about that?</p>

<templating><url><string><conversion>
How can I convert text into a URL-friendly string?
# How can I convert text into a URL-friendly string?

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

If I want to convert text into a URL friendly string for sharing text in a social media link, how would I go about that?

**Tags:** templating, url, string, conversion

---

## 2 answers

### Answer by username_0 on 2014-07-14. Score: 13 | Accepted answer

I was about to ask this question, but realized that it's closely related to this question.
The answer, like in the above referenced question lies with Twig filters.
Specifically, in this case, one needs to use the url_encode filter to convert the text needed.
Example


In any case, I hope that helps others with this. I know how easy it is to forget that the answers sometimes like over in the Twig docs, and not Craft’s.


---

### Answer by username_1 on 2017-08-16. Score: 1

It was clearly documented on Twig Official Documentation that we can use url_encode filter to convert it to url friendly string.
Example
{{ "path-seg*ment"|url_encode }}
{# outputs "path-seg%2Ament" #}

{{ "string with spaces"|url_encode }}
{# outputs "string%20with%20spaces" #}

{{ {'param': 'value', 'foo': 'bar'}|url_encode }}
{# outputs "param=value&foo=bar" #}


---

