php UTF-8 编码的 html 页面显示 ? (问号)代替字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5445137/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:26:55  来源:igfitidea点击:

UTF-8 encoded html pages show ? (questions marks) instead of characters

phphtmlcharacter-encodingxamppspecial-characters

提问by leugim

I have the standard XAMPP installation on win7 (x64). Having had my share of encoding troubles in a past project where mysql encoding did not match with the php enconding which in turn sometimes output html in other encodings, I decided to consistently encode everything using utf-8.

我在 win7 (x64) 上安装了标准的 XAMPP。在过去的项目中,mysql 编码与 php 编码不匹配,而 php 编码有时会以其他编码输出 html,我曾遇到过编码问题,因此我决定始终使用 utf-8 对所有内容进行编码。

I'm just getting started with the html markup and am allready experiencing troubles.

我刚刚开始使用 html 标记,并且已经遇到了麻烦。

  • My page is saved using utf-8 (no BOM, I think)
    //update: It turns out this was NOT the case. The file was actually saved with ISO_8859-1. I later found this out thanks to Sherm Pendleys answer. I had to go back and change my project settings (which were set to "ISO-8859-1") to the desired "UTF-8".
  • php is set per .htaccess to serve .php-pages in utf-8 with: AddCharset UTF-8 .php
  • html has a meta tag specifying: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • To test I set used php header('Content-Type:text/html; charset=UTF-8');
  • 我的页面是使用 utf-8 保存的(我认为没有 BOM )
    //更新:事实证明并非如此。该文件实际上是用ISO_8859-1. 由于 Sherm Pendleys 的回答,我后来发现了这一点。我不得不返回并将我的项目设置(设置为“ISO-8859-1”)更改为所需的“UTF-8”。
  • php 设置为每个 .htaccess 以在 utf-8 中为 .php-pages 提供服务: AddCharset UTF-8 .php
  • html 有一个元标记,指定: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • 为了测试我设置了使用过的 php header('Content-Type:text/html; charset=UTF-8');

The page is evidently served in utf-8 (firefox and chrome recognize it as such) but any special characters such as é, áor ?will just show as ?. Also when viewing the source code.

该页面显然以 utf-8 格式提供(firefox 和 chrome 将其识别为这样),但任何特殊字符,例如é,á?将仅显示为?. 查看源代码时也是如此。

When dropping the encoding settings mentioned above all characters are rendered correctly but the encoding that is detected shows either windows-1252or ISO-8859-1depending on the browser.

当删除上面提到的编码设置时,所有字符都正确呈现,但检测到的编码显示windows-1252ISO-8859-1取决于浏览器。

How come? I'm very puzzled. I would have expected the exact opposite behavior.
Any advice is welcome, thanks!

怎么来的?我很困惑。我会期望完全相反的行为。
欢迎任何建议,谢谢!

edit: Hopefully this helps a bit more. This is the response header (as per firebug)

编辑:希望这会有所帮助。这是响应标头(根据萤火虫)

HTTP/1.1 200 OK
Date: Sat, 26 Mar 2011 20:49:44 GMT
Server: Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.1
Content-Length: 91
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8

采纳答案by Sherm Pendley

When [dropping] the encoding settings mentioned above all characters [are rendered] correctly but the encoding that is detected shows either windows-1252 or ISO-8859-1 depending on the browser.

[删除] 上面提到的编码设置时,所有字符 [已正确呈现] 但检测到的编码显示 windows-1252 或 ISO-8859-1,具体取决于浏览器。

Then that's what you're really sending. None of the encoding settings in your bullet list will actually modify your output in any way; all they do is tell the browser what encoding to assume when interpreting what you send. That's why you're getting those ?s - you're telling the browser that what you're sending is UTF-8, but it's really ISO-8859-1.

那么这就是你真正发送的内容。您的项目符号列表中的任何编码设置都不会以任何方式实际修改您的输出;它们所做的只是告诉浏览器在解释您发送的内容时采用何种编码。这就是为什么你得到那些 ?s - 你告诉浏览器你发送的是 UTF-8,但它实际上是 ISO-8859-1。

回答by Dennis

In my case, database returned latin1, when my browser expected utf8.

就我而言,latin1当我的浏览器预期utf8.

So for MySQLi I did:

所以对于 MySQLi 我做了:

 mysqli_set_charset($dblink, "utf8");    

See http://php.net/manual/en/mysqli.set-charset.phpfor more info

有关更多信息,请参阅http://php.net/manual/en/mysqli.set-charset.php

回答by ErickBest

Tell PDO your charsetinitially.... something like

告诉 PDO 你charset最初......

PDO("mysql:host=$host;dbname=$DB_name;charset=utf8;", $username, $password);

Notice the: charset=utf8;part.

注意:charset=utf8;部分。

hope it helps!

希望能帮助到你!

回答by IProblemFactory

Check if any of your .phpfiles which printing some text, also is correctly encoding in utf-8.

检查您的任何.php打印某些文本的文件是否也正确编码为utf-8.

回答by SPL_Splinter

I'm from Brazil and I create my data bases using latin1_spanish_ci. For the html and everything else I use:

我来自巴西,我使用latin1_spanish_ci. 对于 html 和我使用的其他所有内容:

charset=ISO-8859-1

The data goes right with é,?and ?... Sometimes I have to put the texts of the html using the code of it, such as:

数据与é,??... 有时我必须使用它的代码来放置 html 的文本,例如:

Ol&aacute;

gives me

给我

Olá

You can find the codes in this page: http://www.ascii.cl/htmlcodes.htm

您可以在此页面中找到代码:http: //www.ascii.cl/htmlcodes.htm

Hope this helps. I remember it was REALLY annoying.

希望这可以帮助。我记得这真的很烦人。

回答by Vitalius

Looks like nobody mentioned

好像没人提

SET NAMES utf8;

I found this solution hereand it helped me. How to apply it:

我在这里找到了这个解决方案,它对我有帮助。如何应用它:

To be all UTF-8, issue the following statement just after you've made the connection to the database server: SET NAMES utf8;

要全部使用 UTF-8,请在与数据库服务器建立连接后立即发出以下语句: SET NAMES utf8;

Maybe this will help someone.

也许这会帮助某人。

回答by EmCo

The problem is the charset that is being used by apache to serve the pages. I work with Linux, so I don't know anything about XAMPP. I had the same problem too, what I did to solve the problem was to add the charset to the charsetconfig file (It is commented by default).

问题是 apache 用于为页面提供服务的字符集。我使用 Linux,所以我对 XAMPP 一无所知。我也有同样的问题,我为解决问题所做的是将字符集添加到charset配置文件中(默认情况下是注释)。

In my case I have it in /etc/apache2/conf.d/charsetbut, since you're using Windows the location is different. So I'm giving you this like an idea of how to solve it.

就我而言,我有它,/etc/apache2/conf.d/charset但是,由于您使用的是 Windows,因此位置不同。所以我给你这个就像一个如何解决它的想法。

At the end, my charset config file is like this:

最后,我的字符集配置文件是这样的:

# Read the documentation before enabling AddDefaultCharset.
# In general, it is only a good idea if you know that all your files
# have this encoding. It will override any encoding given in the files
# in meta http-equiv or xml encoding tags.

AddDefaultCharset UTF-8

I hope it helps.

我希望它有帮助。