Twig : url_decode

Brieuc picture Brieuc · Apr 9, 2016 · Viewed 9k times · Source

I encoded an url parameter with the twig filter url_encode.

// app.request.query.get("date") output 01/04/2016

href="{{ path('page', {date: app.request.query.get("date")|url_encode}) }}">

which output in the url

date=01%252F04%252F2016

So in the requested page with url parameters

 {{ app.request.query.get("date") }}

display 01%2F04%2F2016 but i'd like to have 01/04/2016

I tried using raw filter and also did a twig extension :

<?php
namespace SE\AppBundle\Twig;

class htmlEntityDecodeExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('html_entity_decode', array($this, 'htmlEntityDecode'))
        );
    }

    public function htmlEntityDecode($html)
    {
        $html = html_entity_decode($html);
        return $html;
    }

    public function getName()
    {
        return 'html_entity_decode_extension';
    }
}

But even with this it keeps displaying 01%2F04%2F2016

I get the same result in my controller method with :

echo html_entity_decode($request->query->get('date'));

What is the proper way to do this?

UPDATE :

the date is from an input of type 'text'. No this is a simple string with numbers and / .

Answer

Federkun picture Federkun · Apr 9, 2016

There's no need to url-encode the parameter of the query string in the first place, as it's already done by the function that generate the path.

01%252F04%252F2016 is doubly urlencoded. PHP, when gets the request, already decode that value to 01%2F04%2F2016, but since you encoded it twice, it's still urlencoded. You need to use urldecode function to decode it. Or even better: don't urlencode it twice.

It's fine:

{{ path('page', {date: app.request.query.get("date")}) }}

UPDATE

Found this in the source code:

// "/" and "?" can be left decoded for better user experience, see
// http://tools.ietf.org/html/rfc3986#section-3.4
$url .= '?'.(false === strpos($query, '%2F') ? $query : strtr($query, array('%2F' => '/')));

So, the / is left deliberately url-decoded.