将 PHP 默认编码设置为 UTF-8
在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该将
default_charset
设置为 UTF-8:(如果 PHP Cookbook 要求您更改
default_encoding
,PHP Cookbook 中可能存在拼写错误 - 我从未听说过。)如果您要输出 UTF-8 编码字符,您还需要确保您的 Web 服务器设置为输出 UTF-8。在 Apache 中,可以通过在 httpd.conf 文件中进行设置:
至于修改
iconv
、exif
和mssql< /code> 编码设置,您可能不需要设置这些(您的设置已将这些注释掉),但无论如何将它们全部更改为 UTF-8 是个好主意。
You should set your
default_charset
to 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:
As for modifying the
iconv
,exif
, andmssql
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 转换等操作中显式设置编码
另外,请注意保存 PHP 文件的编码,确保它们采用 UTF-8,尤其是当它们包含要显示或比较的字符串时。
Modify the line
to read
About the other options, do not touch them. Avoid default settings, always explicitly set the encoding of in everything you do
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.
我的 MySQL 查询遇到问题,它无法识别某些拉丁语重音符号,因此查询会失败。我以为可能是PHP文件等等,直到我发现使用PDO调用MySQL函数我必须添加字符集。奇怪的是,在我之前使用的服务器上它运行得很好!
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!
在 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
.为了解决这个问题,我将“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.