I have a real mess at my hands with encoding-related bugs.
I have a DB with latin1 (which is close to Windows-1252 I believe), a user frontend page in Windows-1252, and an AJAX backend in Windows-1252. These can't be changed atm.
Yet, because JSON expects UTF8 data, I'm running into tons of trouble with German Umlaute.
I'm currently retrieving some escaped example data from the DB on the frontend [{"\u00f6\u00e4\u00fc\u00df"}] and using
foreach($example_array_of_objects as $k => &$v) {
foreach($v as $k2 => $v2) {
$v[$k2] = utf8_decode($v2);
}
}
which results in correct display of the data in input form fields on the frontend.
However, this is where I'm stuck. PHP's json_encode escapes Umlaute to these \u sequences, but in Javascript, JSON.stringify just doesn't. When I JSON.stringify the input field data and send it to the AJAX script, I get only garbage from a print_r response:
öäüß
encodeURIComponent doesn't do the same type of escaping as PHP does. This is infuriating.
How can I transform "öäüß" to \u00f6\u00e4\u00fc\u00df in JS (or how can I synchronize the way data is handled between JS/PHP/mySQL somehow)?