UTF-8 编码的 html 页面显示 � (问号)而不是字符

发布于 2024-10-27 02:06:19 字数 1350 浏览 1 评论 0原文

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

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

  • 我的页面使用 utf-8 保存(没有 BOM,我认为
    //更新:事实证明情况并非如此。该文件实际上是使用 ISO_8859-1 保存的。后来我通过 Sherm Pendley 的回答才发现了这一点。我必须返回并将我的项目设置(设置为“ISO-8859-1”)更改为所需的“UTF-8”。
  • php 按 .htaccess 设置为以 utf 格式提供 .php 页面-8 with: AddCharset UTF-8 .php
  • html 有一个元标记指定:
  • 为了测试,我设置了使用 php header('Content-Type:text/html; charset=UTF-8');

该页面显然是用 utf-8 提供的(firefox 和 chrome 会这样识别)但任何特殊字符,例如 éá¡ 将仅显示为 �< /代码>。查看源代码时也是如此。

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

怎么会?我很疑惑。我本以为会有完全相反的行为。
欢迎任何建议,谢谢!

编辑: 希望这能有所帮助。这是响应头(根据 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

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.

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

  • 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');

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

抹茶夏天i‖ 2024-11-03 02:06:20

最初告诉 PDO 你的charset...类似

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

注意:charset=utf8; 部分。

希望有帮助!

Tell PDO your charset initially.... something like

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

Notice the: charset=utf8; part.

hope it helps!

煞人兵器 2024-11-03 02:06:20

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

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

牛↙奶布丁 2024-11-03 02:06:20

我来自巴西,使用 latin1_spanish_ci 创建数据库。对于 html 和我使用的其他所有内容:

charset=ISO-8859-1

数据与 éãç 正确...有时我必须输入文本使用它的代码的html,例如:

Olá

给我

Olá

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

希望这会有所帮助。我记得这真的很烦人。

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

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:

Olá

gives me

Olá

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

Hope this helps. I remember it was REALLY annoying.

菊凝晚露 2024-11-03 02:06:20

看起来没有人提到

SET NAMES utf8;

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

要全部使用 UTF-8,请在连接到数据库服务器后发出以下语句:
设置名称 utf8;

也许这会对某人有所帮助。

Looks like nobody mentioned

SET NAMES utf8;

I found this solution here and 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;

Maybe this will help someone.

悍妇囚夫 2024-11-03 02:06:20

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

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

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

# 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

我希望它有帮助。

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 charset config file (It is commented by default).

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

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.

南汐寒笙箫 2024-11-03 02:06:19

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

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

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.

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.

深白境迁sunset 2024-11-03 02:06:19

就我而言,当我的浏览器期望 utf8 时,数据库返回 latin1

所以对于 MySQLi 我做了:

 mysqli_set_charset($dblink, "utf8");    

参见 http://php.net/manual/en/ mysqli.set-charset.php 了解更多信息

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

So for MySQLi I did:

 mysqli_set_charset($dblink, "utf8");    

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文