<p>It looks like the twig <code>url_encode</code> filter uses the php function <code>http_build_query</code> if parameters are passed to it as an array. i.e. <code>{'key':'value', 'key':'value'}|url_encode</code>. Otherwise it uses <code>rawurlencode</code>.</p>

<p><code>http_build_query</code> was modified in php 5.4 to include a new property <code>enc_type</code>, that allowed you to specify that it use <code>PHP_QUERY_RFC3986</code>encoding (which is also what <code>rawurlencode</code> uses).</p>

<p>The <code>url_encode</code> filter is defined in <code>craft/app/vendor/twig/twig/lib/Twig/Extension/Core.php</code> on line 596, and contains a clause to make it backwards compatible with php 5.3, but you obviously will loose the encoding type. </p>

<p>Options are: upgrade php to 5.4+; do not pass params as an array to the <code>url_encode</code> filter; or do a string replace on the encoded string.</p>

CC BY-SA 3.0
2014-11-13T15:58:31.313Z
I was just going to say that! ;)
Brad Bell
CC BY-SA 3.0
2014-11-13T05:37:16.793Z
2014-11-13T14:46:07.850Z
2014-11-13T14:46:07.850Z
Douglas McDonald
<p>I just uploaded some templates to the server and discovered that some of my url encoded strings are being output with a "+" for spaces instead of the "%20" that is desired. It is fine in my local environment. Any ideas?</p>

<p>According to the <a href="http://twig.sensiolabs.org/doc/filters/url_encode.html" rel="nofollow">twig encode_url documentation</a> <code>url_encode</code> now uses php's <code>rawurlencode</code> by default:</p>

<blockquote>
  <p>Internally, Twig uses the PHP urlencode (or rawurlencode if you pass true as the first parameter) or the http_build_query function. Note that as of Twig 1.16.0, urlencode always uses rawurlencode (the raw argument was removed.)</p>
</blockquote>

<p>I have also tried passing 'true' as the first parameter just in case. No luck.</p>

CC BY-SA 3.0
2014-11-12T21:11:52.057Z
What PHP versions are on the server and locally?
Brad Bell
CC BY-SA 3.0
2014-11-12T21:26:47.953Z
@Brad local 5.5.14; server 5.3.5
Douglas McDonald
CC BY-SA 3.0
2014-11-12T21:29:17.023Z
Was thinking maybe it's a PHP change in behavior/bug fix, but not seeing anything here http://php.net/ChangeLog-5.php
Brad Bell
CC BY-SA 3.0
2014-11-13T01:15:27.713Z
If you test `url_encode()` directly from a PHP script with the same string, do you see the same behavior?
Brad Bell
CC BY-SA 3.0
2014-11-13T01:48:57.357Z
@BradBell Sorry, I'm not sure how to call `url_encode()` from php. Is there a craft helper function available? Or do you mean php's `urlencode()` and `rawurlencode()`? Both of those act as expected on both the server and local. urlencode returns '+' and rawurlencode returns '%20'.
Douglas McDonald
CC BY-SA 3.0
2014-11-13T01:54:16.977Z
Yeah... sorry. I meant PHP's `urlencode()`. Craft 2.2 is using Twig 1.16.0, so it should be using PHP's `rawurlencode()`. Will have to think on it some more.
Brad Bell
CC BY-SA 3.0
2014-11-13T05:40:42.703Z
@BradBell I think I got it — The filter is actually using `http_build_query` when params are passed as an array, which was changed in php 5.4.
Douglas McDonald
<templating>
Why would one environment use "+" instead of "%20" for spaces when using url_encode filter?
# Why would one environment use "+" instead of "%20" for spaces when using url_encode filter?

(Asked by: username_0 on 2014-11-12)

I just uploaded some templates to the server and discovered that some of my url encoded strings are being output with a "+" for spaces instead of the "%20" that is desired. It is fine in my local environment. Any ideas?

According to the [twig encode\_url documentation](http://twig.sensiolabs.org/doc/filters/url_encode.html) `url_encode` now uses php's `rawurlencode` by default:

> Internally, Twig uses the PHP urlencode (or rawurlencode if you pass true as the first parameter) or the http\_build\_query function. Note that as of Twig 1.16.0, urlencode always uses rawurlencode (the raw argument was removed.)

I have also tried passing 'true' as the first parameter just in case. No luck.

**Tags:** templating
> **Comments:**
> **username_1**: What PHP versions are on the server and locally?  
> **username_0**: @Brad local 5.5.14; server 5.3.5  
> **username_1**: Was thinking maybe it's a PHP change in behavior/bug fix, but not seeing anything here http://php.net/ChangeLog-5.php  
> **username_1**: If you test `url_encode()` directly from a PHP script with the same string, do you see the same behavior?  
> **username_0**: @BradBell Sorry, I'm not sure how to call `url_encode()` from php. Is there a craft helper function available? Or do you mean php's `urlencode()` and `rawurlencode()`? Both of those act as expected on both the server and local. urlencode returns '+' and rawurlencode returns '%20'.  
> **username_1**: Yeah... sorry. I meant PHP's `urlencode()`. Craft 2.2 is using Twig 1.16.0, so it should be using PHP's `rawurlencode()`. Will have to think on it some more.  
> **username_0**: @BradBell I think I got it — The filter is actually using `http_build_query` when params are passed as an array, which was changed in php 5.4.  

---

## 1 answers

### Answer by username_0 on 2014-11-13. Score: 1 | Accepted answer

It looks like the twig `url_encode` filter uses the php function `http_build_query` if parameters are passed to it as an array. i.e. `{'key':'value', 'key':'value'}|url_encode`. Otherwise it uses `rawurlencode`.

`http_build_query` was modified in php 5.4 to include a new property `enc_type`, that allowed you to specify that it use `PHP_QUERY_RFC3986`encoding (which is also what `rawurlencode` uses).

The `url_encode` filter is defined in `craft/app/vendor/twig/twig/lib/Twig/Extension/Core.php` on line 596, and contains a clause to make it backwards compatible with php 5.3, but you obviously will loose the encoding type.

Options are: upgrade php to 5.4+; do not pass params as an array to the `url_encode` filter; or do a string replace on the encoded string.

> **Comments:**
> **username_1**: I was just going to say that! ;)  

---

