Pergunta

As I site in the title I have accented characters encoding problem. I found several solutions on the web but it does not work or I do not know how to use. at the beginning: I collect all the data in php myssql base and I use the json_encode function returns the data to the application. This is where it crashes. all fields where there is a character with accents is replaced by null. data base is either a website (which I can not change it) and it is encoded in windows-1252 itself directly inserted with wampPHP why I found a solution

$liste_amis = $bdd->query('SELECT * FROM lieux');



            $nombre_amis=0;
            while($myliste=$liste_amis->fetch()){


                foreach($myliste as &$value)
                    {
                     $value = mb_convert_encoding($value, "UTF-8", "Windows-1252");
                    }

                $output[]=array_map('utf8_encode', $myliste );




                 } 
print(json_encode($output));

but it works only for data inserted with wampPHP more it appears like this "discotA¨que" and in Logcat like this "discoth\u00c3\u00a8que"

and leftovers are always given the null

in my java code I use the BufferedReader

 try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8192);
            StringBuilder sb = new StringBuilder();
            String line = null;
            int i = 0;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                i++;

Help

Edit :

Finally it works, solution :

mysql_query ( "'utf8' SET NAMES" );
//--....... 
 while($myliste=$liste_amis->fetch()){
$output[]=array_map('utf8_encode', $myliste);
}
print(json_encode($output,JSON_UNESCAPED_UNICODE));

java :

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);

tanks.

Foi útil?

Solução

Rewrite your code to this...

foreach($myliste as &$value)
{
    $value = mb_convert_encoding($value, "UTF-8", "Windows-1252");
}
$output[]=array_map('utf8_encode', $myliste);
}
print(json_encode($output,JSON_UNESCAPED_UNICODE));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top