How to properly display Chinese characters in PHP?

前端 未结 5 1996
清酒与你
清酒与你 2020-12-04 01:39

I have these Chinese characters:

汉字/漢字\'\'test

If I do

echo utf8_encode($chinesevar);

it displays

<
相关标签:
5条回答
  • 2020-12-04 01:56

    save your source code in UTF-8 No BOM

    0 讨论(0)
  • 2020-12-04 01:57

    Look that your file is in UTF8 without BOM and that your webserver deliver your site in UTF-8

    HTML:

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

    in PHP:

    header('Content-Type: text/html; charset=utf-8');
    

    And if you work with a database look that your database is in UTF-8 if you read the text from your database.

    0 讨论(0)
  • 2020-12-04 01:58

    Perhaps take a look at the following solutions:

    1. Your database, table and field COLLATE should be utf8_unicode_ci
    2. Check if your records are showing the correct characters within the database...
    3. Set your html to utf8

    4. Add the following line to your php after connecting to the database

      mysqli_set_charset($con,"utf8");

    http://www.w3schools.com/php/func_mysqli_set_charset.asp

    0 讨论(0)
  • 2020-12-04 02:01

    Simple:

    1. save your source code in UTF-8
    2. output an HTTP header to specify to your browser that it should interpret the page using UTF-8:

      header('Content-Type: text/html; charset=utf-8');
      

    Done.

    utf8_encode is for converting Latin-1 encoded strings to UTF-8. You don't need it.

    For more details, see Handling Unicode Front To Back In A Web App.

    0 讨论(0)
  • 2020-12-04 02:04

    $chinesevarOK = mb_convert_encoding($chinesevar, 'HTML-ENTITIES', 'UTF-8');

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