php PHP如何正确显示汉字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13496271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly display Chinese characters in PHP?
提问by sasori
I have these Chinese characters:
我有这些汉字:
汉字/漢字''test
If I do
如果我做
echo utf8_encode($chinesevar);
it displays
它显示
??/??''test
Or even if I just do a simple
或者即使我只是做一个简单的
echo $chinesevar
it still displays some weird characters...
它仍然显示一些奇怪的字符......
So how am I going to display these Chinese characters without using the <meta> tag with the UTF-8 thingy .. or the ini_set UTF-8 thing or even the header() thing with UTF-8?
那么我将如何在不使用带有 UTF-8 的 <meta> 标签的情况下显示这些中文字符 .. 或 ini_set UTF-8 的东西,甚至是带有 UTF-8 的 header() 的东西?
回答by deceze
Simple:
简单的:
- save your source code in UTF-8
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');
- 以 UTF-8 格式保存源代码
输出一个 HTTP 标头以向浏览器指定它应该使用 UTF-8 解释页面:
header('Content-Type: text/html; charset=utf-8');
Done.
完毕。
utf8_encodeis for converting Latin-1 encoded strings to UTF-8. You don't need it.
utf8_encode用于将 Latin-1 编码的字符串转换为 UTF-8。你不需要它。
For more details, see Handling Unicode Front To Back In A Web App.
有关更多详细信息,请参阅在 Web 应用程序中从前到后处理 Unicode。
回答by René H?hle
Look that your file is in UTF8 without BOM and that your webserver deliver your site in UTF-8
看看您的文件是没有 BOM 的 UTF8 格式,并且您的网络服务器以 UTF-8 格式传送您的网站
HTML:
HTML:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
in PHP:
在 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.
如果您使用数据库,如果您从数据库中读取文本,则您的数据库是 UTF-8 格式的。
回答by Stephan
Perhaps take a look at the following solutions:
也许看看以下解决方案:
- Your database, tableand fieldCOLLATE should be utf8_unicode_ci
- Check if your records are showing the correct characters within the database...
Set your html to utf8
Add the following line to your php after connecting to the database
mysqli_set_charset($con,"utf8");
- 您的数据库、表和字段COLLATE 应该是 utf8_unicode_ci
- 检查您的记录是否在数据库中显示正确的字符...
将您的 html 设置为 utf8
连接到数据库后,将以下行添加到您的php
mysqli_set_charset($con,"utf8");

