php UTF-8 字符显示不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5733336/
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
UTF-8 characters don't display correctly
提问by Theo Smeets
This is my PHP code:
这是我的 PHP 代码:
<?php
$result = '';
$str = 'Тугайный соловей';
for ($y=0; $y < strlen($str); $y++) {
$tmp = mb_substr($str, $y, 1);
$result = $result . $tmp;
}
echo 'result = ' . $result;
The output is:
输出是:
D¢??D3D°D1D???D1 ?D?D?D?D2DμD1
What can I do? I have to put $result
into a MySQL database.
我能做什么?我必须放入$result
一个MySQL数据库。
回答by Capsule
What's the encoding of your file? It should be UTF8 too. What's the default charset of your http server? It should be UTF-8 as well.
你的文件编码是什么?它也应该是UTF8。你的 http 服务器的默认字符集是什么?它也应该是 UTF-8。
Encoding only works if:
编码仅在以下情况下有效:
- the file is encoded correctly
- the server tells what's the encoding of the delivered file.
- 文件编码正确
- 服务器告诉传送文件的编码是什么。
When working with databases, you also have to set the right encoding for your DB fields andthe way the MySQL client communicates with the server (see mysql_set_charset()
). Fields only are not enough because your MySQL client (in this case, PHP) could be set to ISO by default and reinterprets the data. So you end up with UTF8 DB -> ISO client -> injected into UTF8 PHP script. No wonder why it's messed up at the end :-)
使用数据库时,您还必须为数据库字段以及MySQL 客户端与服务器通信的方式设置正确的编码(请参阅 参考资料mysql_set_charset()
)。仅字段是不够的,因为您的 MySQL 客户端(在本例中为 PHP)可以默认设置为 ISO 并重新解释数据。所以你最终得到了 UTF8 DB -> ISO 客户端 -> 注入到 UTF8 PHP 脚本中。难怪为什么最后搞砸了:-)
How to serve the file with the right charset?
如何使用正确的字符集提供文件?
header('Content-type: text/html; charset=utf-8')
is one solution
header('Content-type: text/html; charset=utf-8')
是一种解决方案
.htaccess file containing AddDefaultCharset UTF-8
is another one
.htaccess 文件包含AddDefaultCharset UTF-8
另一个
HTML meta content-type might work too but it's always better to send this information using HTTP headers.
HTML 元内容类型也可能有效,但使用 HTTP 标头发送此信息总是更好。
PS: you also have to use mb_strlen()
because strlen()
on UTF8 strings will probably report more than the real length.
PS:您还必须使用,mb_strlen()
因为strlen()
在 UTF8 字符串上可能会报告超过实际长度。
回答by Nemoden
I suppose, your code is in windows-1251
encoding since it is Russian :)
convert your string to utf-8:
我想,您的代码正在windows-1251
编码,因为它是俄语:) 将您的字符串转换为 utf-8:
$str = iconv('windows-1251', 'utf-8', $str);
回答by Stevie
If you're going to send a mix of data and don't want to specify utf-8 using a php header, you can add this html to your page:
如果您要发送混合数据并且不想使用 php 标头指定 utf-8,则可以将此 html 添加到您的页面:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
回答by Kiva
If your database is UTF-8, it's ok for mysql.
如果您的数据库是 UTF-8,那么 mysql 就可以了。
For your echo, if you do it in a web site, put this in the top page:
对于你的回声,如果你在网站上做,把它放在首页:
header('Content-Type: text/html; charset=UTF-8');
回答by Alireza S.T.
try this:
尝试这个:
header('Content-Type: text/html; charset=UTF-8');
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
//print "$name_field\n$data";
// ?? ??? ?? ???? ??
print chr(255) . chr(254) . mb_convert_encoding("$name_field\n$data", 'UTF-16LE', 'UTF-8');
回答by Behnam Chaghajerdi
Just add this line at the beginning, after the connection with server:
只需在与服务器连接后的开头添加此行:
mysqli_set_charset($conn,"utf8");
回答by ralphb
if you are just using php echo with no html headers etc., this worked great for me.
如果您只是使用没有 html 标题等的 php echo,这对我来说非常有用。
$connect = mysqli_connect($host_name, $user_name, $password, $database); mysqli_set_charset($connect,"utf8");
$connect = mysqli_connect($host_name, $user_name, $password, $database); mysqli_set_charset($connect,"utf8");