2

I need to display data which is stored in Postgresql database and encoded in SQL_ASCII (no way I can change the database encoding). The application is written in PHP (using Silex framework and Doctrine DBAL), end generally the charset being used in the application is UTF8.

Is there any way I can automatically convert the data from ASCII into UTF8 encoding and display the data properly?

Thanks!

EDIT: data in the postgresql database is stored in ISO-8859-2 encoding.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
farfocello
  • 21
  • 2

2 Answers2

2

UTF8 is deliberately designed to be compatible with true 7-bit ASCII, so any character in your database from those 128 characters is already valid UTF8, so can just be used as is.

However, as explained in the Postgres manual page on encodings, the SQL_ASCII character set doesn't actually reject characters outside the ASCII range:

When the server character set is SQL_ASCII, the server interprets byte values 0-127 according to the ASCII standard, while byte values 128-255 are taken as uninterpreted characters. ... Thus, this setting is not so much a declaration that a specific encoding is in use, as a declaration of ignorance about the encoding.

So if you have any non-ASCII characters in your data, you'll need to know what actual character set they are stored in (e.g. ISO 8859-15 or Windows CP1252) and convert them to UTF8 using something like iconv() or mb_convert_encoding().

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • 1
    That's what I was afraid of... :) Yeah, actual character set is ISO-8859-2, and the preffered way would be to do the conversion automatically. – farfocello Feb 21 '15 at 16:49
  • @farfocello "Automatic" is relative - it just means it's happening out of sight of the code you're writing right now. There might be a way to add the conversion logic to the DBAL or ORM layer, for instance. – IMSoP Feb 21 '15 at 17:25
0

You can use the function iconv. This question has already been asked here.

Community
  • 1
  • 1
André Ferraz
  • 1,511
  • 11
  • 29
  • It's PostgreSQL DB, not MySQL. – farfocello Feb 21 '15 at 16:48
  • With `iconv` you can turn a string represented by a local character set into the one represented by another character set. – André Ferraz Feb 21 '15 at 16:53
  • When pointing out an existing/duplicate question, it's best to do so as a comment rather than an answer. Once you have sufficient reputation, you can vote to officially mark the question as a duplicate. – IMSoP Feb 21 '15 at 16:53
  • I'd prefer to make the conversion on the fly, as there are lots of places where I'd have to add the iconv conversion manually. – farfocello Feb 21 '15 at 16:58