将 PHP 默认编码设置为 utf-8?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9351694/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:38:41  来源:igfitidea点击:

Setting PHP default encoding to utf-8?

phpunicodeencodingcharacter-encoding

提问by JDelage

In the "PHP Cookbook", they say (p.589) that to properly set the char encoding of outgoing data to utf-8 it is necessary to edit the default_encodingconfiguration to utf-8.

在“PHP Cookbook”中,他们说 (p.589) 要将输出数据的字符编码正确设置为 utf-8,必须将default_encoding配置编辑为 utf-8。

However, I cannot find this configuration in php.ini. Should I simply add a line that would say default_encoding = "utf-8"?

但是,我在php.ini. 我应该简单地添加一行会说default_encoding = "utf-8"吗?

I do have a ;default_charset = "iso-8859-1". As you can see (;), right now it is not activated. Should I remove the semi-colon and set it to "utf-8"? Does that take care of the default encoding?

我有一个;default_charset = "iso-8859-1". 如您所见 ( ;),现在它没有被激活。我应该删除分号并将其设置为"utf-8"吗?这会处理默认编码吗?

I also found other encoding directives that I don't know what to do about:

我还发现了其他我不知道该怎么做的编码指令:

[iconv]
;iconv.input_encoding = ISO-8859-1
;iconv.internal_encoding = ISO-8859-1
;iconv.output_encoding = ISO-8859-1
...
; http://php.net/exif.encode-unicode
;exif.encode_unicode = ISO-8859-15
...
;mssql.charset = "ISO-8859-1"
...
;exif.encode_unicode = ISO-8859-15

Is there any reason why I shouldn't simply replace them all with utf-8?

有什么理由我不应该简单地将它们全部替换为utf-8

回答by Ben D

You should set your default_charsetto UTF-8:

您应该将您的设置default_charset为 UTF-8:

default_charset = "utf-8"

(PHP Cookbook may have a typo in it if they ask you to change the default_encoding— I've never heard of it.)

(如果 PHP Cookbook 要求您更改它,则其中可能有错字default_encoding— 我从未听说过。)

You'll also want to make sure that your webserver is set to output UTF-8 if you're going to outputting UTF-8 encoded characters. In Apache this can be set by in the httpd.conf file:

如果您要输出 UTF-8 编码的字符,您还需要确保您的网络服务器设置为输出 UTF-8。在 Apache 中,这可以在 httpd.conf 文件中设置:

AddDefaultCharset UTF-8

As for modifying the iconv, exif, and mssqlencoding settings, you probably don't need to set these (your settings have these commented out anyhow) but it's a good idea to change them all to UTF-8 anyhow.

至于修改 iconvexifmssql编码的设置,你可能并不需要这些设置(设置有这些注释无论如何),但它是一个好主意,他们都更改为UTF-8进不去。

回答by gioele

Modify the line

修改行

;default_charset = "iso-8859-1"

to read

阅读

default_charset = "utf-8"

About the other options, do not touch them. Avoid default settings, always explicitly set the encoding of in everything you do

关于其他选项,不要碰它们。避免默认设置,始终明确设置您所做的一切的编码

  • database connections,
  • reading and writing files,
  • converting with iconv.
  • 数据库连接,
  • 读取和写入文件,
  • 用 iconv 转换。

Also, beware of the encoding in which your PHP files are saved, make sure that they are in UTF-8, especially if they contain strings to be displayed or compared.

另外,请注意保存 PHP 文件的编码,确保它们是 UTF-8,尤其是当它们包含要显示或比较的字符串时。

回答by Miguel

I had a problem on my mysql query that it would not recognize some latin acentuation, so the query would fail. I thought it could be the php file and so on, till i found out that using pdo to call the mysql i had to add the charset. The weird thing is that the previous server i used worked fine!

我的 mysql 查询有一个问题,它无法识别某些拉丁文,因此查询将失败。我以为它可能是 php 文件等等,直到我发现使用 pdo 调用 mysql 我必须添加字符集。奇怪的是我以前使用的服务器运行良好!

$dsn = 'mysql:host=localhost;dbname=retirodo_main;charset=utf8';
- - - - - - - - - - - - - - - - - - - - - - - - - ^^^^^^^^^^^^

回答by user7232321

To resolve I changed "UTF-8" to "UTF-8" (without the dash), solving the problem instead.

为了解决,我将“UTF-8”更改为“UTF-8”(没有破折号),而是解决了问题。

CentOS

CentOS

Izaias Moura

伊萨亚斯·莫拉

回答by ontananza

At htaccess level (.htaccess file) the php directive should be php_value default_charset UTF-8

在 htaccess 级别(.htaccess 文件),php 指令应该是 php_value default_charset UTF-8