没有BOM的UTF-8 html显示奇怪的字符 [英] UTF-8 html without BOM displays strange characters

查看:115
本文介绍了没有BOM的UTF-8 html显示奇怪的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些HTML,其中包含一些外来字符(€,ó,á). HTML文档另存为UTF-8,不带BOM.当我在浏览器中查看页面时,外来字符似乎已被陌生的字符组合(â,³,Ã)代替.只有当我将HTML文档另存为带有BOM表的UTF-8时,字符才能正确显示.

I have some HTML which contains some forign characters (€, ó, á). The HTML document is saved as UTF-8 without BOM. When I view the page in the browser the forign characters seem to get replaced with stranger character combinations (€, ó, Ã). It's only when I save my HTML document as UTF-8 with BOM that the characters then display properly.

我真的希望不必在文件中包含BOM,但是有人知道为什么会这样做吗?并解决它? (不包括物料清单)

I'd really rather not have to include a BOM in my files, but has anybody got any idea why it might do this? and a way to fix it? (other than including a BOM)

推荐答案

您可能未在HTML文件中指定正确的字符集. BOM(感谢@Jukka)使浏览器进入UTF-.8模式.如果没有它,则需要使用其他方式来声明文档UTF.8.

You are probably not specifying the correct character set in your HTML file. The BOM (thanks @Jukka) sends the browser into UTF-.8 mode; in its absence, you need to use other means to declare the document UTF.8.

如果您有权访问服务器配置,则可能要确保服务器没有发送错误的字符集信息.参见例如如何将默认编码更改为UTF-8对于Apache?

If you have access to your server configuration, you may want to make sure the server isn't sending the wrong character set info. See e.g. How to change the default encoding to UTF-8 for Apache?

如果您只能访问HTML,则可以在文档的头部添加此meta标记,从而达到目的:

If you have access only to your HTML, adding this meta tag in your document's head should do the trick:

<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>

或@Mathias指出,新的HTML 5

or as @Mathias points out, the new HTML 5

<meta charset="utf-8"> 

(仅当您使用HTML 5文档类型时才有效,即使您不使用HTML 5标记,该文档类型也不再具有良好的论据.)

(valid only if you use a HTML 5 doctype, against which there is no good argument any more even if you don't use HTML 5 markup.)

这篇关于没有BOM的UTF-8 html显示奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆