How to pass latin1 associative attribute set from php to javascript?

The problem is that I have latin1 charset in db. The default output is utf-8 in php. even when I use php iconv, it returns null since the new line is supposedly already utf-8.

Converting a DB will require downtime.

I cannot use json_encode because the string has the wrong form utf-8 containing.

I need an alternative to fix the problem temporarily without db migration.

I need to pass an associative array back to JS. I use Ajax to get data.

0
source share
2 answers

You can convert the encoding before transferring the data to json_encode (which really needs UTF8 encoded strings). Using the mb_convert_encoding function will do:

 $utf8String = mb_convert_encoding( $sourceString, 'UTF8', '<src encoding>' //in your case: ISO-8859-1 ); 

Meanwhile, work out a strategy for how you are going to convert your database to UTF-8, because it will save you a lot of pain by endlessly manipulating various encodings throughout your project.

0
source

Configure the driver (client) encoding explicitly on UTF-8.

Examples with:

0
source

Source: https://habr.com/ru/post/974201/


All Articles