使用PHP将HTTP标头设置为UTF-8

本文讨论了如何解决PHP生成的HTML页面字符编码与HTTP头部不符的问题,提供了解决方案,包括通过PHP修改HTTP头部设置为UTF-8,并强调了修改的时机和检查已发送头部的方法。
摘要由CSDN通过智能技术生成

本文翻译自:Set HTTP header to UTF-8 using PHP

I have several PHP pages echoing out various things into HTML pages with the following code. 我有几个PHP页面使用以下代码将各种内容回显到HTML页面中。

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

However, when I validate using the W3C validator it comes up with: 但是,当我使用W3C验证器验证时,它会出现:

The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8). HTTP标头(iso-8859-1)中指定的字符编码与元素(utf-8)中的值不同。

I am quite new to PHP, and I was wondering if I could and should change the header for the PHP files to match the HTML files. 我是PHP的新手,我想知道我是否可以并且应该更改PHP文件的标头以匹配HTML文件。


#1楼

参考:https://stackoom.com/question/HxEg/使用PHP将HTTP标头设置为UTF


#2楼

You can also use a shorter way: 您也可以使用更短的方式:

<?php header('Content-Type: charset=utf-8'); ?>

See RFC 2616 . 请参阅RFC 2616 It's valid to specify only character set. 仅指定字符集是有效的。


#3楼

For a correct implementation, you need to change a series of things. 为了正确实现,您需要更改一系列内容。

Database (immediately after the connection): 数据库(连接后立即):

mysql_query("SET NAMES utf8");

// Meta tag HTML (probably it's already set): 
meta charset="utf-8"
header php (before any output of the HTML):
header('Content-Type: text/html; charset=utf-8')
table-rows-charset (for each row):
utf8_unicode_ci

#4楼

Use header to modify the HTTP header: 使用header修改HTTP标头:

header('Content-Type: text/html; charset=utf-8');

Note to call this function before any output has been sent to the client. 注意在将任何输出发送到客户端之前调用此函数。 Otherwise the header has been sent too and you obviously can't change it any more. 否则标题也已发送,您显然无法再更改它。 You can check that with headers_sent . 您可以使用headers_sent进行检查。 See the manual page of header for more information. 有关详细信息,请参阅header手册页


#5楼

First make sure the PHP files themselves are UTF-8 encoded. 首先确保PHP文件本身是UTF-8编码的。

The meta tag is ignored by some browser. 某些浏览器会忽略元标记。 If you only use ASCII-characters, it doesn't matter anyway. 如果你只使用ASCII字符,那么无论如何都没关系。

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

header('Content-Type: text/html; charset=utf-8');

#6楼

This is a problem with your web server sending out an HTTP header that does not match the one you define. 这是您的Web服务器发送的HTTP标头与您定义的HTTP标头不匹配的问题。 For instructions on how to make the server send the correct headers, see this page . 有关如何使服务器发送正确标头的说明,请参阅此页面

Otherwise, you can also use PHP to modify the headers, but this has to be done before outputting any text using this code: 否则,您也可以使用PHP来修改标题,但必须使用此代码输出任何文本之前完成此操作:

header('Content-Type: text/html; charset=utf-8');

More information on how to send out headers using PHP can be found in the documentation for the header function . 有关如何使用PHP发送标头的更多信息可以在头文件功能的文档中找到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值