Can't convert encoding from us-ascii to utf-8. What am screwing up?

2

I'm trying to debug garbage characters that my webpages are rendering in foreign languages. I want my createpage.php in utf-8.

me@host:/www/webroot$ file -bi createpage.php
text/x-php; charset=us-ascii
me@host:/www/webroot$ iconv -f us-ascii -t utf-8 createpage.php > createpage_utf8.php
me@host:/www/webroot$ file -bi createpage_utf8.php
text/x-php; charset=us-ascii

I have rwx permessions on the directory, rw on the files. I must be blind - what am I screwing up here? Is it because ascii is a subset of utf-8 and my php doesn't have any special characters (just the ini files that I include), so file -bi just reports ascii anyway?

Escher

Posted 2015-03-06T14:26:00.290

Reputation: 309

Answers

2

A file using only plain ASCII characters is indistinguishable from UTF8, as by definition the 7-bit ASCII characters are mapped 1-to-1 in UTF8. Hence your "conversion" doesn't do anything.

wurtel

Posted 2015-03-06T14:26:00.290

Reputation: 1 359

Well I feel a little silly. I guess my problem lies with php's PDO module then, since all my tables are encoded in UTF-8. Thanks for taking the time to answer. – Escher – 2015-03-06T14:57:41.520

Make sure that the webserver is passing on that the content is UTF-8 encoded, e.g. by adding a config line AddDefaultCharset UTF-8 – wurtel – 2015-03-06T15:05:58.350

$db->exec("SET CHARACTER SET utf8"); directly after opening the PDO connection to the database may help, with mysql. – wurtel – 2015-03-06T15:07:32.830

All the relevant tables were already utf8 and the webserver was set to utf8, as were the http headers. It turns out I had just forgotten the charset=utf8 argument when instantiating the PDO object. – Escher – 2015-03-06T15:15:26.537