0

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 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.

  • What PHP versions are on the server and locally? – Brad Bell Nov 12 '14 at 21:11
  • @Brad local 5.5.14; server 5.3.5 – Douglas McDonald Nov 12 '14 at 21:26
  • Was thinking maybe it's a PHP change in behavior/bug fix, but not seeing anything here php.net/ChangeLog-5.php – Brad Bell Nov 12 '14 at 21:29
  • If you test url_encode() directly from a PHP script with the same string, do you see the same behavior? – Brad Bell Nov 13 '14 at 1:15
  • @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 Nov 13 '14 at 1:48
1

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_RFC3986encoding (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.

| improve this answer | |
  • I was just going to say that! ;) – Brad Bell Nov 13 '14 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.