UTF-8 problem

I have a table news. Inside I have a column text with utf8_unicode_ci encoding set. Some native old php application is doing database insert and my yii2 application is responsible for reading.

This is a sentence that needs to be displayed in my application ( among others ):


Igračke, Školski asortiman, Papirna galanterija, Dečje knjige, Dečja odeća

In database it is saved like this:


Igračke, Školski asortiman, Papirna galanterija, Dečje knjige, Dečja odeća 

When I do mb_detect_encoding on the returned output from the database it is saying that this is UTF-8.

If I do utf8_decode on the output I get almost good result:


Igračke, Školski asortiman, Papirna galanterija, Dečje knjige, Dečja ode�?a 

Problems are these two question marks in the last word.

I have set these to try to get proper output but nothing:


<meta charset="UTF-8"/>

and in index.php file:


header('Content-Type: text/html; charset=utf-8');

Why I am not getting good output ? In old not yii application everything is displayed correctly…

Just a thought: Check which charsets have been set on the database connection in your legacy app. And possible in your new app as well.

I have figured out that I need to comment out the charset in db configuration:


'class' => 'yii\db\Connection',

    'dsn' => 'mysql:host=localhost;dbname=asd',

    'username' => 'asd',

    'password' => 'asd',

    //'charset' => 'utf8',

But because my colleagues made a mess with encodings in different tables sometimes I need to set charset on db config, and sometimes I don’t. Do you have any tip of what would be the best practice in these kinds of situations ? Some of my models are using ‘charset’ => ‘utf8’ and some must not. Creating separate connection for all of them is kinda heavy and sloppy thing to do… ?

Is the legacy app still being used? Because if not, spare yourself the hassle and convert the tables in question.