字符集 UTF-8 和 php header() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32967768/
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
Charset UTF-8 and php header() not working
提问by Gjert
I am having some problems with the charset on my website. I can't get the UTF-8 charset to work and I get ? instead of ?. Yes, I have searched around the web for answers and I've already used the following code:
我的网站上的字符集有一些问题。我无法让 UTF-8 字符集工作,我得到 ? 代替 ?。是的,我在网上搜索了答案,并且已经使用了以下代码:
<!DOCTYPE HTML>
<head>
<?php header('Content-type: text/html; charset=utf-8'); ?>
<meta http-equiv="Content-Type" content="text/html" charset=utf-8" />
<title><?php echo $db_title; ?></title>
</head>
<body>
Some content with letters ???
</body>
</html>
The database uses latin1_swedish_ci
.
数据库使用latin1_swedish_ci
.
Any idea what it could be?
知道它可能是什么吗?
采纳答案by Martin
Set the PHP
header
tag before any output to the browser (as pointed out by JJ Cantillon)While PHP and HTML use the correct UTF-8, it is worth noting that MySQL uses a reduced version of UTF-8 and so when selecting the Collation of the database tables/fields the "utf8_general_ci" (and similar) are notfull character sets and so your characters you want to display will not be stored correctly.
header
在任何输出到浏览器之前设置 PHP标记(如 JJ Cantillon 所指出的)虽然 PHP 和 HTML 使用正确的 UTF-8,但值得注意的是 MySQL 使用简化版本的 UTF-8,因此在选择数据库表/字段的排序规则时,“utf8_general_ci”(和类似的)不是完整的字符集因此您要显示的字符将无法正确存储。
What you need to do is to ensure MySQL uses the complete UTF-8 character set by selecting
您需要做的是通过选择确保 MySQL 使用完整的 UTF-8 字符集
Collation = 'utf8mb4'
整理 = 'utf8mb4'
Also, when you are connecting your script to the database you need to specify the same (utf8mb4
) character set for PHP/MySQL communication - so set something like (in as far as MySQLi or PDO etc.)
此外,当您将脚本连接到数据库时,您需要utf8mb4
为 PHP/MySQL 通信指定相同的 ( ) 字符集 - 因此设置类似(就 MySQLi 或 PDO 等而言)
new PDO('mysql:host=localhost;charset=utf8mb4')
or for MySQLi read up on http://php.net/manual/en/mysqli.set-charset.php
或 MySQLi 阅读http://php.net/manual/en/mysqli.set-charset.php
If you store data in the SQL as utf8mb4
but only transfer the data using standard MySQL-utf8 (3bytes rather than 4bytes) then the character sets will be screwed up in transport.
如果您将数据存储在 SQL 中,utf8mb4
但仅使用标准 MySQL-utf8(3 字节而不是 4 字节)传输数据,那么字符集将在传输中搞砸。
回答by JJ Cantillon
You will need to set the header before you output any html.
在输出任何 html 之前,您需要设置标题。
Try to set the header before your first HTML tag. EG:
尝试在您的第一个 HTML 标记之前设置标题。例如:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html" charset=utf-8" />
<title><?php echo $db_title; ?></title>
</head>
<body>
Some content with letters ???
</body>
</html>
回答by Gjert
After a lot of searching I came across the following question.In the database connection I had to specify that I wanted the UTF-8 charset. I did it the following way:
经过大量搜索,我遇到了以下问题。在数据库连接中,我必须指定我想要 UTF-8 字符集。我是通过以下方式做到的:
$db = new PDO('mysql:host=localhost;dbname=NAME;charset=utf8', 'USERNAME', 'PASSWORD');
It must have been a server update from the host that caused the problem, since I haven't been affected by the problem before now.
一定是来自主机的服务器更新导致了问题,因为我之前没有受到问题的影响。