url_encode
url_encode
过滤器对给定字符串进行百分号编码,用作 URL 段,或将映射编码为查询字符串
1 2 3 4 5 6 7 8
{{ "path-seg*ment"|url_encode }}
{# outputs "path-seg%2Ament" #}
{{ "string with spaces"|url_encode }}
{# outputs "string%20with%20spaces" #}
{{ {'name': 'Fabien', 'city': 'Paris'}|url_encode }}
{# outputs "name=Fabien&city=Paris" #}
注意
在内部,Twig 使用 PHP 的 rawurlencode 或 http_build_query 函数。