12

我有这些汉字:

汉字/漢字''test

如果我做

echo utf8_encode($chinesevar);

它显示

??/??''test

或者即使我只是做一个简单的

echo $chinesevar

它仍然显示一些奇怪的字符...

那么,如果不使用带有 UTF-8 的 <meta> 标记 .. 或带有 UTF-8 的 ini_set UTF-8 甚至 header() 的东西,我将如何显示这些汉字呢?

4

5 回答 5

15

简单的:

  1. 将源代码保存为 UTF-8
  2. 输出一个 HTTP 标头以向您的浏览器指定它应该使用 UTF-8 解释页面:

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

完毕。

utf8_encode用于将 Latin-1 编码的字符串转换为 UTF-8。你不需要它。

有关更多详细信息,请参阅在 Web 应用程序中前后处理 Unicode

于 2012-11-21T15:20:04.227 回答
2

看看您的文件是 UTF8 没有 BOM 并且您的网络服务器以 UTF-8 交付您的网站

HTML:

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

在 PHP 中:

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

如果您使用数据库,如果您从数据库中读取文本,请查看您的数据库是 UTF-8 格式。

于 2012-11-21T15:21:01.557 回答
1

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

于 2020-09-09T10:29:53.817 回答
0

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

于 2016-06-02T12:21:41.393 回答
0

将源代码保存为 UTF-8 无 BOM

于 2020-08-30T04:06:28.067 回答