Charset=utf8 在我的 PHP 页面中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10460349/
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=utf8 not working in my PHP page
提问by Tepken Vannkorn
I got a problem with my character encoding. Whenever I load a page in the browser, It shows like this:

我的字符编码有问题。每当我在浏览器中加载页面时,它显示如下:

So I need to manually configure it by the browser. Thanks for your help.
所以我需要通过浏览器手动配置它。谢谢你的帮助。
回答by oezi
sounds like you don't serve your content as utf-8. do this by setting the correct header:
听起来您的内容没有以 utf-8 格式提供。通过设置正确的标题来做到这一点:
header('Content-type: text/html; charset=utf-8');
header('Content-type: text/html; charset=utf-8');
in addition to be really sure the browser understands, add a meta-tag:
除了确保浏览器理解之外,添加一个元标记:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
note that, depending on where the text comes from, you might have to check some other things too (database-connection, source-file-encoding, ...) - i've listed a lot of them in one of my answers to a similar question.
请注意,根据文本的来源,您可能还需要检查其他一些内容(数据库连接、源文件编码等) - 我在我的一个答案中列出了很多一个类似的问题。
回答by NullPoiиteя
As stated by kraikkonen85 in this comment:
正如 kraikkonen85 在此评论中所述:
Besides setting
mysql_set_charsetand addingutf8setting metadata to html, another important but "unpredictable" reason is the encoding type of your PHP editor when saving. For example, if you save the php file other than encodings such as "unicode" or "ANSI", as i experienced, you may notice weird chars like squares, question marks etc.To make sure, try "save as" method to see explictly that your php file is being saved as
utf8.
除了为 html设置
mysql_set_charset和添加utf8设置元数据之外,另一个重要但“不可预测”的原因是保存时 PHP 编辑器的编码类型。例如,如果您保存的 php 文件不是编码(如“unicode”或“ANSI”),正如我所经历的,您可能会注意到奇怪的字符,如正方形、问号等。为了确保,请尝试“另存为”方法以明确查看您的 php 文件是否被另存为
utf8.
it may be because your content might not utf-8 you can set utf-s by setting the header and meta
可能是因为你的内容可能不是 utf-8 你可以通过设置 header 和 meta 来设置 utf-s
header('Content-type: text/html; charset=utf-8');
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

