Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Judging from everything you've said, it seems like the original <code>Odómetro</code> string you're dealing with is encoded with ISO 8859-1, not UTF-8.</p> <p>Here's why I think so:</p> <ul> <li><code>json_encode</code> produced parseable output after you ran the input string through <code>utf8_encode</code>, which converts from ISO 8859-1 to UTF-8.</li> <li>You did say that you got "mangled" output when using <code>print_r</code> after doing <code>utf8_encode</code>, but the mangled output you got is actually exactly what would happen by trying to parse UTF-8 text as ISO 8859-1 (ó is <code>\x63\xb3</code> in UTF-8, but that sequence is <code>ó</code> in ISO 8859-1.</li> <li>Your <code>htmlentities</code> hackaround solution worked. <code>htmlentities</code> needs to know what the encoding of the input string to work correctly. If you don't specify one, it assumes ISO 8859-1. (<code>html_entity_decode</code>, confusingly, defaults to UTF-8, so your method had the effect of converting from ISO 8859-1 to UTF-8.)</li> <li>You said you had the same problem in Python, which would seem to exclude PHP from being the issue.</li> </ul> <p>PHP will use the <code>\uXXXX</code> escaping, but as you noted, this is valid JSON.</p> <p>So, it seems like you need to configure your connection to Postgres so that it will give you UTF-8 strings. The PHP manual indicates you'd do this by appending <code>options='--client_encoding=UTF8'</code> to the connection string. There's also the possibility that the data currently stored in the database is in the wrong encoding. (You could simply use <code>utf8_encode</code>, but this will only support characters that are part of ISO 8859-1).</p> <p>Finally, as another answer noted, you do need to make sure that you're declaring the proper charset, with an HTTP header or otherwise (of course, this particular issue might have just been an artifact of the environment where you did your <code>print_r</code> testing).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload