5 回答
the working solution for me was to follow the twig documentation http://twig.sensiolabs.org/doc/filters/convert_encoding.html
applying a filter {{ field|convert_encoding('UTF-8', ' iso-8859-1') }}
because this often happen when your sub- template is not inheriting the main one , where you had the correct meta charset defined
I'm not sure that your source data is actually UTF-8 encdoded. var_dump()
alone isn't sufficient to verify the encoding of your content.
Try echoing/dumping the content and viewing the page with Firefox or Chrome and change the encoding to UTF-8 and then something else like ISO-8859-1. If your content looks wrong when you have UTF-8 selected then your content is not UTF-8 encoded.
- To change the encoding in Firefox: View > Character Encoding
- To change the encoding in Chrome: Tools > Encoding
I had similar issue and in my case the problem was database encoding mismatch. The solution for me was to use utf8_encode()
when first inserting the data into database.
Sample from my code:
// Convert file name into title of template
$name = str_replace(array('-', '_'), ' ', basename($file, ".html"));
$name = utf8_encode($name);
mb_detect_encoding($str) is also a method to detect encoding.
Try this filter: convert_encoding('UTF-8', 'ISO-8859-1')