0

I have some info in my database. I subsequently echo json_encode($array); the array in which the info is contained and return it to the page displaying the content via AJAX/jQuery.

For example;

"Dave's apples are sour".

My webpage displays it as:

"Dave’s apples are sour".

This is obviously a character encoding issue. we've never had issues until my hosting company was acquired by a different company.

The header of the page in which the content is displayed contains:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Since then we've tried the following:

  • mysqli_set_charset($conn, "utf8"); <- does not solve the problem
  • echo json_encode( $array, JSON_UNESCAPED_UNICODE ); <- does not solve the problem

A echo mb_detect_encoding(); on the specific variable that contains "Dave's apples are sour" echoes: "UTF-8".

When examining the console of the page on which the info is displayed reveals that the content arrives to the script "messed up", FireBug shows the response literally being: Dave’s apples are sour.

The column in which the data is contained has the following structure:

ID, Name, Type, Collations

6, BlogPost, text, utf8_general_ci

Does anybody have some new insights? I have been searching for hours on StackOverflow without result.

Julian Koster
  • 262
  • 1
  • 10
  • Have you verified with a plain MySQL client, entirely out of PHP context that the original text in the database is correctly encoded, and wasn't inserted to a column incapable of handling utf8? – Michael Berkowski Aug 29 '18 at 12:33
  • Here is also a "definitive" guide: https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – BenRoob Aug 29 '18 at 12:34
  • how about `header('Content-Type: text/html; charset=utf-8')` ? – apokryfos Aug 29 '18 at 12:50
  • @apokryfos unfortunately no success – Julian Koster Aug 29 '18 at 12:57
  • 2
    Trace it step by step. Database → PHP, PHP → browser, Javascript → ??? → display (whatever you're doing there exactly…). Use `echo bin2hex(...)` to see the raw bytes you're dealing with, which helps in narrowing it down at what point the problem occurs. Rule out all the steps in which the string contains the correct bytes. – deceze Aug 29 '18 at 13:04
  • Most likely the host is now setting a different default HTTP `Content-Type` header, making the browser interpret the data in a different encoding. – deceze Aug 29 '18 at 13:06

1 Answers1

-1

Use to encode:

json_encode($data,JSON_UNESCAPED_UNICODE)

and to decode:

json_decode($json,false, 512, JSON_UNESCAPED_UNICODE);

  • Unfortunately the data is processed by jQuery in the end, not by PHP – Julian Koster Aug 29 '18 at 13:02
  • 1) `JSON_UNESCAPED_UNICODE` would only make the issue *worse*, if anything. 2) `JSON_UNESCAPED_UNICODE` is not a valid/useful flag for `json_decode`. – deceze Aug 29 '18 at 13:05