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

时间:2022-10-24 23:39:45

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_encoding configuration 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替换它们呢?

6 个解决方案

#1


39  

You should set your default_charset to 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.)

(如果他们要求您更改default_encoding, PHP Cookbook中可能有一个输入错误——我从未听说过它。)

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编码的字符,还需要确保webserver设置为输出UTF-8。在Apache中,这可以在httpd中设置。配置文件:

AddDefaultCharset UTF-8

As for modifying the iconv, exif, and mssql encoding 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.

至于修改iconv、exif和mssql编码设置,您可能不需要设置这些(您的设置已经注释掉了这些),但最好还是将它们全部更改为UTF-8。

#2


6  

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中,特别是当它们包含要显示或比较的字符串时。

#3


3  

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查询有一个问题,它无法识别某些拉丁acentuation,因此查询将失败。我认为它可以是php文件等等,直到我发现使用pdo调用mysql时我必须添加字符集。奇怪的是,我之前使用的服务器运行得很好!

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

#4


0  

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

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

CentOS

CentOS

Izaias Moura

Izaias莫拉

#5


0  

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

htaccess级别(。php指令应该是php_value default_charset UTF-8

#6


-2  

changing from *default_charset = "utf-8"* to *default_charset = "iso-8859-1"* works for me ( i use Apache web server and Linux OS). for some reasons when i use utf-8, some characters don't render well. They either change to some square sign or question mark depending on the web browser used.

从*default_charset = "utf-8"*更改为*default_charset = "iso-8859-1"*对我来说是可行的(我使用Apache web服务器和Linux OS)。由于某些原因,当我使用utf-8时,有些字符不能很好地呈现。根据所使用的web浏览器,它们要么更改为某个方形符号,要么更改为问号。

#1


39  

You should set your default_charset to 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.)

(如果他们要求您更改default_encoding, PHP Cookbook中可能有一个输入错误——我从未听说过它。)

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编码的字符,还需要确保webserver设置为输出UTF-8。在Apache中,这可以在httpd中设置。配置文件:

AddDefaultCharset UTF-8

As for modifying the iconv, exif, and mssql encoding 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.

至于修改iconv、exif和mssql编码设置,您可能不需要设置这些(您的设置已经注释掉了这些),但最好还是将它们全部更改为UTF-8。

#2


6  

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中,特别是当它们包含要显示或比较的字符串时。

#3


3  

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查询有一个问题,它无法识别某些拉丁acentuation,因此查询将失败。我认为它可以是php文件等等,直到我发现使用pdo调用mysql时我必须添加字符集。奇怪的是,我之前使用的服务器运行得很好!

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

#4


0  

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

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

CentOS

CentOS

Izaias Moura

Izaias莫拉

#5


0  

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

htaccess级别(。php指令应该是php_value default_charset UTF-8

#6


-2  

changing from *default_charset = "utf-8"* to *default_charset = "iso-8859-1"* works for me ( i use Apache web server and Linux OS). for some reasons when i use utf-8, some characters don't render well. They either change to some square sign or question mark depending on the web browser used.

从*default_charset = "utf-8"*更改为*default_charset = "iso-8859-1"*对我来说是可行的(我使用Apache web服务器和Linux OS)。由于某些原因,当我使用utf-8时,有些字符不能很好地呈现。根据所使用的web浏览器,它们要么更改为某个方形符号,要么更改为问号。