I know there are plenty of related topics about this issue, but I haven't been able to fix the problem with any of them.
I have a MySQL table with words and some of them can contain scandinavian letters, such as å, ä and ö. When I output them simply with echo
or print_r()
, the output is always �. I have tried using utf8_encode()
, which shows a different invalid result. Using mb_detect_encoding()
, I have noticed the encoding of the words containing these letters is UTF-8 already.
Example words:
A = the word (and expected output)
B = echo word
C = echo utf8_encode(word)
D = mb_detect_encoding(word)
E = mb_detect_encoding(utf8_encode(word))
+-------+-------+-------+-------+-------+
| A | B | C | D | E |
+-------+-------+-------+-------+-------+
| word | word | word | ASCII | ASCII |
| työ | ty� | tyã¶ | UTF-8 | UTF-8 |
| ylä | yl� | yl㤠| UTF-8 | UTF-8 |
+-------+-------+-------+-------+-------+
The collation of all of my MySQL tables is set to utf8 - utf8_swedish_ci
and when initializing PDO I have
$dbh = new PDO("mysql:host=xxxx;dbname=yyyy;charset=utf8", "zzzz", "****");
$dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
Also, the encoding of all of my files is set to UTF-8 without BOM and before outputting I have header("Content-Type: text/html; charset=UTF-8");
Using ini_set('default_charset', 'UTF-8');
in the beginning of PHP file does nothing.
So, the question is - how can I actually output the words correctly? I'd also like to know why is utf8_encode()
changing the output from wrong (UTF-8) to different wrong (still UTF-8) so I'd actually learn something about this mess called encoding.