Twig UTF-8 Raw Content Encoding

The controller passes the array to the branch template. The array contains strings that correctly encode UTF-8. I can check this in the controller using var_dump($theArray) . All lines are displayed correctly.

But in the branch

 {% for video in videos %} {{ video.title_value | raw }} <br> {% endfor %} 

some characters, such as Γ–,Γ„,Ü , are replaced by . Controller and template encoded UTF-8 without BOM and

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

. I have to make the initial output, because the lines can contain html tags. Any idea how to fix

+4
source share
5 answers

I'm not sure if your source data is actually UTF-8 encoded. var_dump() not enough to check the encoding of your content.

Try to redo / reset the content and view the page using Firefox or Chrome and change the encoding to UTF-8, and then something else like ISO-8859-1. If your content does not look right, if you chose UTF-8, then your content is not encoded in UTF-8 encoding.

  • To change the encoding in Firefox: View> Character encoding
  • To change the encoding in Chrome: Tools> Encoding
+2
source

The working solution for me was to follow the documentation http://twig.sensiolabs.org/doc/filters/convert_encoding.html

filter application {{field | convert_encoding ('UTF-8', 'iso-8859-1')}}

because this often happens when your sub-subsequence does not inherit the main one, where you specified the correct meta-set of characters

+3
source

I had a similar problem, and in my case the problem was a database encoding mismatch. The solution for me was to use utf8_encode() when I first inserted the data into the database.

An example from my code:

 // Convert file name into title of template $name = str_replace(array('-', '_'), ' ', basename($file, ".html")); $name = utf8_encode($name); 
0
source

mb_detect_encoding ($ str) is also an encoding detection method.

http://php.net/mb_detect_encoding

0
source

Try this filter: convert_encoding('UTF-8', 'ISO-8859-1')

0
source

Source: https://habr.com/ru/post/1411211/


All Articles