json_encode won't encode French characters

后端 未结 5 1577
感动是毒
感动是毒 2020-12-06 15:22

I\'m trying to work with French characters. For whatever reason, PHP won\'t output them correctly unless I force the ISO-8859-1 character set (which I really don\'t

相关标签:
5条回答
  • 2020-12-06 15:57

    Please, besides the database encoding, be sure you check the following:

    • utf8 encoding of the FILES (js/php)
    • utf8 html content: <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
    • utf8 of your db connection: SET character_set_connection = 'utf8'
    • utf8 of your mysql tables: ALTER TABLE table CONVERT TO CHARACTER SET utf8;
    • utf8 of your query results: SET character_set_results = 'utf8'
    • utf8 of your db client: SET character_set_client = 'utf8'
    • utf8 of your db server: SET character_set_database = 'utf8' and SET character_set_server = 'utf8'
    • in some cases, forcing utf8 in file is necessary when hardcoded values need encoding. You would need to add a comment on top of your php/js/xml file for instance, with charset=utf-8, so ultra edit or your favorite editor can detect it.

    rgds.

    0 讨论(0)
  • 2020-12-06 15:58

    basic; has your .php file the line on top?

    header("content-type:text/html;charset=utf8\n");

    0 讨论(0)
  • 2020-12-06 15:58

    I have run into the same issue, but I would suggest:

    $array = htmlentities($array);
    $json = json_encode($array);
    
    0 讨论(0)
  • 2020-12-06 15:59

    I managed to figure it out. It's not really the solution I wanted but it works. I had to adjust my query to look like:

    CONVERT(CAST(langselect as BINARY) USING latin1) as langselect
    
    0 讨论(0)
  • 2020-12-06 16:00

    Try to work on sources directly in utf8.

    I was faced with the same problem, and used this simple solution: When you create a new PHP or Javascript source file, ensure it is encoded in utf8. I use Ultra Edit, and chose the option “UTF8 no bom”. This simple step resolved all my problems with encoding characters.

    All modern browsers support utf8, so encode your webpages with this. For compatibility, it is simpler if all your source files are also in utf8.

    This follows web standards.

    0 讨论(0)
提交回复
热议问题