将 PHP 默认编码设置为 UTF-8

发布于 2025-01-06 17:05:09 字数 751 浏览 0 评论 0原文

在“PHP Cookbook”中,他们说(第 589 页)要将传出数据的字符编码正确设置为 UTF-8,需要将 default_encoding 配置编辑为 utf- 8

但是,我在文件 php.ini 中找不到此配置。我是否应该简单地添加一行 default_encoding = "utf-8"

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

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

[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

是否有任何理由我不应该简单地将它们全部替换为 utf-8

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

However, I cannot find this configuration in file php.ini. Should I simply add a line that would say 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 semicolon and set it to "utf-8"? Does that take care of the default encoding?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倦话 2025-01-13 17:05:09

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

default_charset = "utf-8"

如果 PHP Cookbook 要求您更改 default_encodingPHP Cookbook 中可能存在拼写错误 - 我从未听说过。)

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

AddDefaultCharset UTF-8

至于修改 iconvexifmssql< /code> 编码设置,您可能不需要设置这些(您的设置已将这些注释掉),但无论如何将它们全部更改为 UTF-8 是个好主意。

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

You'll also want to make sure that your web server 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:

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.

冰雪梦之恋 2025-01-13 17:05:09

修改该行

;default_charset = "iso-8859-1"

以读取

default_charset = "utf-8"

“关于其他选项”,请勿触摸它们。 使用 iconv 转换等操作中显式设置编码

  • 避免默认设置,始终在数据库连接、
  • 读写文件、

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

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.

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.

暗恋未遂 2025-01-13 17:05:09

我的 MySQL 查询遇到问题,它无法识别某些拉丁语重音符号,因此查询会失败。我以为可能是PHP文件等等,直到我发现使用PDO调用MySQL函数我必须添加字符集。奇怪的是,在我之前使用的服务器上它运行得很好!

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

I had a problem on my MySQL query that it would not recognize some Latin accentuation, 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 function I had to add the character set. The weird thing is that on the previous server I used it worked fine!

$dsn = 'mysql:host=localhost;dbname=retirodo_main;charset=utf8';
- - - - - - - - - - - - - - - - - - - - - - - - - ^^^^^^^^^^^^
梦里兽 2025-01-13 17:05:09

htaccess 级别(.htaccess 文件),PHP 指令应为 php_value default_charset UTF-8

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

深爱成瘾 2025-01-13 17:05:09

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

它位于 CentOS 上。

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

It was on CentOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文