--- /dev/null
+``convert_encoding``
+====================
+
+.. versionadded:: 1.4
+ The ``convert_encoding`` filter was added in Twig 1.4.
+
+The ``convert_encoding`` filter converts a string from one encoding to
+another. The first argument is the expected output charset and the second one
+is the input charset:
+
+.. code-block:: jinja
+
+ {{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
+
+.. note::
+
+ This filter relies on the `iconv`_ or `mbstring`_ extension. So one of
+ them must be installed.
+
+.. _`iconv`: http://php.net/iconv
+.. _`mbstring`: http://php.net/mbstring
'replace' => new Twig_Filter_Function('twig_strtr'),
// encoding
- 'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'),
- 'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'),
+ 'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'),
+ 'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'),
+ 'convert_encoding' => new Twig_Filter_Function('twig_convert_encoding'),
// string filters
'title' => new Twig_Filter_Function('twig_title_string_filter', array('needs_environment' => true)),
// escape all non-alphanumeric characters
// into their \xHH or \uHHHH representations
if ('UTF-8' != $charset) {
- $string = _twig_convert_encoding($string, 'UTF-8', $charset);
+ $string = twig_convert_encoding($string, 'UTF-8', $charset);
}
if (null === $string = preg_replace_callback('#[^\p{L}\p{N} ]#u', '_twig_escape_js_callback', $string)) {
}
if ('UTF-8' != $charset) {
- $string = _twig_convert_encoding($string, $charset, 'UTF-8');
+ $string = twig_convert_encoding($string, $charset, 'UTF-8');
}
return $string;
}
if (function_exists('iconv')) {
- function _twig_convert_encoding($string, $to, $from)
+ function twig_convert_encoding($string, $to, $from)
{
return iconv($from, $to, $string);
}
} elseif (function_exists('mb_convert_encoding')) {
- function _twig_convert_encoding($string, $to, $from)
+ function twig_convert_encoding($string, $to, $from)
{
return mb_convert_encoding($string, $to, $from);
}
} else {
- function _twig_convert_encoding($string, $to, $from)
+ function twig_convert_encoding($string, $to, $from)
{
throw new Twig_Error_Runtime('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
}