Html 单引号在浏览器中显示为菱形问号(无数据库或 PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11788712/
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
Single quotes showing as diamond shaped question mark in browsers (no database or PHP)
提问by blackcatweb
I am working with a web page in which I switched the character set from iso-8859-1 to utf-8. The top of the page reads like this:
我正在使用一个网页,其中我将字符集从 iso-8859-1 切换到 utf-8。页面顶部是这样写的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>[title of site]</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I am only using ASCII characters in the page, and since utf-8 encoding supersets ASCII, this should be fine. However, single quotes in the text are showing up as question marks surrounded by black diamonds. I have verified these are are ASCII single quotes (not straight quotes).
我只在页面中使用 ASCII 字符,并且由于 utf-8 编码超集 ASCII,所以这应该没问题。但是,文本中的单引号显示为被黑色菱形包围的问号。我已经验证这些是 ASCII 单引号(不是直引号)。
I've read much online that describes solutions to the problem that involve PHP, magic quotes, database configuration, etc. However, this is a flat HTML page that isn't being rendered by any programs.
我在网上阅读了很多描述涉及 PHP、魔术引号、数据库配置等问题的解决方案的文章。然而,这是一个没有被任何程序呈现的平面 HTML 页面。
Also, many who have this problem are told to switch to UTF-8 to fix the problem. This is exactly how I introducedthe problem.
此外,许多遇到此问题的人被告知切换到 UTF-8 以解决该问题。这正是我介绍问题的方式。
Please look at http://mch.blackcatwebinc.com/src/events.htmlto see this problem.
采纳答案by prosfilaes
The only quotes in ASCII are the single quote ' (0x27 or 39) and the double quote " (0x22 or 33). What you have there is an 8-bit encoding that places quotes at 145 (0x91) and 146 (0x92) called CP1252; it's the standard 8-bit Western European encoding for Windows. If what you want is UTF-8, you need to convert that to UTF-8, since it's not valid UTF-8; valid UTF-8 uses multiple bytes for characters above 127 (0x7F), and places the opening and closing quotes at U+2018 and U+2019 respectively.
ASCII 中唯一的引号是单引号 '(0x27 或 39)和双引号“(0x22 或 33)。您拥有的是一种 8 位编码,将引号放在 145(0x91)和 146(0x92)处,称为CP1252;它是 Windows 的标准 8 位西欧编码。如果您想要的是 UTF-8,则需要将其转换为 UTF-8,因为它不是有效的 UTF-8;有效的 UTF-8 使用多个字节作为字符高于 127 (0x7F),并将开盘价和收盘价分别置于 U+2018 和 U+2019。
回答by Jason Gennaro
According to the W3C, the meta charset
根据 W3C 的说法, meta charset
should appear as close as possible to the top of the head element
应该尽可能靠近头部元素的顶部
From http://www.w3.org/International/questions/qa-html-encoding-declarations#metacontenttype
来自http://www.w3.org/International/questions/qa-html-encoding-declarations#metacontenttype
So, I might try to place the meta
tag above the title
.
所以,我可能会尝试将meta
标签放在title
.
Also, as mentioned in the first answer by @user1505373, UTF
is always capitalized and there is no space after the =
in any of the examples I saw.
另外,正如@user1505373 在第一个答案中提到的,在我看到的任何示例中,UTF
始终大写,并且在 之后没有空格=
。
回答by deceze
Your source code is notsaved in UTF-8 but Latin1CP1252, and those quotes are notsimple quotes but U+2019 RIGHT SINGLE QUOTATION MARKS (encoded in Latin1). Save the source file in UTF-8 and it'll work.
你的源代码不是用 UTF-8 保存的,而是用Latin1CP1252保存的,这些引号不是简单的引号,而是 U+2019 右单引号(用 Latin1 编码)。以 UTF-8 格式保存源文件,它会起作用。
回答by Jukka K. Korpela
The simplest fix is to change UTF-8 to windows-1252 in the meta
tag. This works, because the server announces no encoding in the Content-Type
header, so browsers and other clients will use the one specified in a meta
tag.
最简单的解决方法是将meta
标签中的 UTF-8 更改为 windows-1252 。这是有效的,因为服务器在Content-Type
标头中宣布没有编码,因此浏览器和其他客户端将使用meta
标签中指定的编码。
The name windows-1252 is the preferred MIME name for the 8-bit Windows Latin-1 encoding, also known as cp1252 and some other names (often misrepresented as “ANSI”).
名称 windows-1252 是 8 位 Windows Latin-1 编码的首选 MIME 名称,也称为 cp1252 和其他一些名称(通常误表示为“ANSI”)。
As @deceze explains, the actual encoding of the data is windows-1252, not UTF-8. You can alternatively change the actual encoding to UTF-8 by saving the file with a suitable command in your authoring software. But what really matters is that the declared encoding matches the real one.
正如@deceze 解释的那样,数据的实际编码是 windows-1252,而不是 UTF-8。您也可以通过在创作软件中使用合适的命令保存文件,将实际编码更改为 UTF-8。但真正重要的是声明的编码与真实编码相匹配。
Yet another possibility is to use “escapes” for the apostrophe, such as ’
. They work independently of encoding, but they make the source code less legible.
另一种可能性是对撇号使用“转义”,例如’
. 它们独立于编码工作,但它们使源代码不太清晰。
回答by Xhezairi
All ASCII printable characters have their equivalent HTML Entity Code. Some of these characters are generally supported by most common OS typefaces, some are categorized as Symbols that bring us to your rendering issue.
所有 ASCII 可打印字符都有其等效的 HTML 实体代码。其中一些字符通常被大多数常见的操作系统字体支持,一些被归类为符号,让我们解决您的渲染问题。
What you supposedly have there is a closing single quote, and in order to get it rightly printed you should use it's entity code, or ’ respectively. If it turns to be an opening single quote, then you should use ‘ instead.
你应该有一个结束单引号,为了正确打印它,你应该使用它的实体代码,或者 分别。如果它变成了一个开头的单引号,那么你应该使用 反而。
Note, there's no HTML Entity Name for the two ASCIIcharacters (and some more) so you're required to opt the entity code variant.
请注意,这两个ASCII字符(以及更多字符)没有 HTML 实体名称,因此您需要选择实体代码变体。
回答by user1505373
The only difference I see between your tag and the one on the site I'm working on is the space after the semicolon and that utf is lowercase on yours. Try capitalizing UTF.
我看到你的标签和我正在处理的网站上的标签之间的唯一区别是分号后面的空格和你的 utf 是小写的。尝试将 UTF 大写。