Search code examples
phpmysqljoomlajoomla2.5

Can't get the encoding to use UTF-8, even with the <metadata> tag?


I converted a Joomla 1.5 site to 2.5, and just about everything is going well, but I'm fighting the display of a character. On the live site there is text like “You Agency guys are twisted.”, and it displays fine, but on the converted site that same database article displays like �You Agency guys are twisted.�.

Now, I've verified that the two elements have the exact same computed style. But it feels like a font issue. Is it maybe an issue with how Joomla 2.5 encodes that character?

Edit 1

After the comments I went and verified the databse. I found that the character_set_database for the old database was latin1 and not utf8, so I ran this statement:

alter database my_database default charset latin1;

and now all character set variables line up between the two database. Further, I verified that the character set for the page is utf-8 because it's emitting this <metadata> tag:

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

However, the characters still aren't displaying properly.

Edit 2

I've also tried setting my default_charset to utf-8 for PHP:

default_charset = "utf-8"

Before adding that line there wasn't even a setting, so it would be the Apache default I'm guessing.

Edit 3

I've now also verified that the index.php file has a Content-Type header of text/html; charset=utf-8. But alas, these special characters still aren't displaying right.


Solution

  • Try running this query before anything else:

    mysql_query("SET NAMES 'utf8'");
    

    The last time I had problems with character sets it was because even though everything else was in UTF-8 (the HTML being served, the code I wrote, the database tables, etc.), the database connection was still passing everything in another charset. This should fix that.

    Edit: as mentioned by @null.point3r below, using the following code is a better alternative because escaped strings might otherwise still use the wrong encoding:

    $mysqli->set_charset('utf8')